Reputation: 21877
I have two tables:
Users and Groups
a User has_many groups and a group, belongs_to a user:
u = User.last
u.groups
Supposed I wanted a second list of different groups, for some strange reason. Where once again a User has may groups (called other_group in this example) and a group belongs to a User.
u = User.last
u.other_groups
How do I associate two models in this relationship, Twice using Active Record?
Upvotes: 1
Views: 773
Reputation: 96454
You can do
class User
has_many :groups, :class_name => "Group", :foreign_key => "group_id"
has_many :other_groups, :class_name => "Group", :foreign_key => "other_group_id"
Upvotes: 0
Reputation: 96454
You can do
User(user_id)
Group(group_id)
UserGroup (id, user_id, group_id)
this allows you to have records with user_ids associated for different groups.
This way lets you have multiple user-group associations.
Upvotes: 0
Reputation: 96454
Your User model could have two foreign keys (attributes in rails)
User.group_id
User.other_group_id
Upvotes: 1