seed_dump icon indicating copy to clipboard operation
seed_dump copied to clipboard

has_and_belongs_to_many support?

Open rschmitty opened this issue 11 years ago • 6 comments

Is there a way to get the records of the join tables from a has_and_belongs_to_many association?

For example, I'd like to get the assemblies_parts dumped in addition to assemblies and parts in the rails example: http://guides.rubyonrails.org/association_basics.html#the-has_and_belongs_to_many-association

rschmitty avatar May 13 '13 10:05 rschmitty

+1

drewww avatar Aug 01 '13 15:08 drewww

is this issue addressed? . Is there any way to get dump of join tables?

psaxena avatar Jan 30 '14 10:01 psaxena

Not yet. I haven't had the need/time to add support for HABTM, though I of course would be happy to merge any pull requests that provide support :)

I'm leaving this issue open as I would like to have support for HABTM at some point.

rroblak avatar Feb 04 '14 18:02 rroblak

In Rails 4.1, HABTM is now implemented with has_many :through so this now pretty much works. The only problem is that you get a psuedo-model from both sides of the relationship, so I threw this in a rake task:

    # Dedupe models on table name so HABTM tables don't get dumped twice.
    Rails.application.eager_load!
    ENV['MODELS'] =
      ActiveRecord::Base.descendants.
        uniq {|c| c.table_name}.
        map(&:to_s).
        join(',')
    Rake::Task['db:seed:dump'].execute

mschulkind avatar Sep 03 '14 22:09 mschulkind

Also see pull request #101. This is essentially the same as mschulkind's solution, but applied automatically, and only to auto-generated HABTM_* models. Is there any case in which you would not want this behavior?

GarthSnyder avatar May 17 '16 23:05 GarthSnyder

This would be a cool feature!

darkBuddha avatar Dec 23 '21 11:12 darkBuddha