Reputation: 2795
I have a products model in my rails app, and with in the products table is a categories column, which is comma separated. I want to create a join table (product_categories) so that the categories are no longer comma separated (making it easier to count etc). Im am planning on joining by id on the product table and have id, product_id and categories on the join table.
Is it best practice to run a migration to create a join table or should I generate a model then run the migration to create the join table?
I am running rails 3.1.0.
Upvotes: 3
Views: 3928
Reputation: 51062
Either approach can work; this is basically the difference between has_many :through and has_and_belongs_to_many.
If you need to interact with the join itself as a model (e.g. put validations on it, add additional metadata, etc.) then you should generate a model for the join, and define the relationship as has_many :through.
However, in your case, it doesn't seem likely that you'll need to add extra information about a given product-category join, so it may make more sense to just create a migration for the join table and use a has_and_belongs_to_many relation.
Upvotes: 2