I am new to rails, and am trying to set up a many-to-many relationship in my rails project. I have a small strategy, but I am not sure if its the correct way.
Aim: I have a table of users, and a table of groups. Users can be part of many groups, and each group may have many users.
Strategy:
- Set up User migration to have name:string
- Set up Group migration to have name:string
- Set up a Join table migration
- Set up User model such that it would have has_and_belongs_to_many :groups
- Set up Group model such that it would have has_and_belongs_to_many :users
Would this be the correct strategy? Thanks!
Railcast Summary from answer: For those that are interested - Railcast suggests you to use a has_many :through association since the strategy above has the limitation that you cannot add extra relation-specific information.
check out: http://kconrails.com/tag/has_many/