pboling/seed_migration

Document Support for has_and_belongs_to_many (HABTM) tables

prem-prakash opened this issue ยท 7 comments

Is it possible to add support to has_and_belongs_to_many tables that has no model ?

Hi,

This is currently not supported (I guess the README could be more explicit about this), as we use model classes to generate the seed file.

I think this would be a great feature, but sadly I can't guarantee that I'll have time to work on this anytime soon.

As usual, I'd be very happy to take a look at a PR of you want to implement this feature :)

Dug into this a bit this evening, and it appears that the has_and_belongs_to_many join table does actually get a pair of implicit backing models. Given the following:

class Product
  has_and_belongs_to_many :categories
end

class Category
  has_and_belongs_to_many :products
end

We get two backing HABTM models: Product::HABTM_Categories and Category::HABTM_Products. As far as I can tell (having implemented a test for this on my fork), registering either of these models should do the trick.

SeedMigration.register Category::HABTM_Products

I'd be happy to submit a PR with the test and some documentation for this.

@tbloncar still interested in submitting a PR for this?

I also found that the implicit model exists (though it's a private constant). However, when I register the model, I'm getting an error because the join table does not have an id column

 HABTM_PathogenListVersions Load (1.3ms)  SELECT `citations_pathogen_list_versions`.* FROM `citations_pathogen_list_versions` ORDER BY id
Mysql2::Error: Unknown column 'id' in 'order clause' excluded from capture: Not configured to send/capture in environment 'development'

I tried to exclude the id field as well, but that did not seem to help. It looks like the model query includes an order by ID clause: https://github.com/pboling/seed_migration/blob/85100250c9f241779cb600f1df4030ca19a6b65c/lib/seed_migration/migrator.rb#L258C31-L258C43

I am happy to review a PR, but I won't have time to work on this for some time.

Yea, I forked the repo and am planning to work on a fix today. I'll link it here and open a PR when ready.

I opened a PR (#80) from a branch in my fork. I had meant to just do that in my fork and then merge it upstream, but let me know if this is fine, or if you'd prefer that I redo the PR.

Also, I have no idea if the original poster still needs this, but this PR would probably address #23 as well