0

I have a users model and a groups model.

A user has_and_belongs_to_many :groups and a group has_and_belongs_to_many :users. The associations and join tables work correctly.

A group can only have one user who owns the group, I want to name be able to call @group.owner to return the user that owns the group but am unsure of how to do it, I've created a user references column on the Group table rails g migration add_user_to_groups user:references but I don't know what to put in the Group model, something like has_one :user, as: :owner

Is this the correct way to do it?

raphael_turtle
  • 7,154
  • 10
  • 55
  • 89

1 Answers1

2
has_one :owner, class_name: "User", foreign_key: 'user_id'
Sully
  • 14,672
  • 5
  • 54
  • 79
manu29.d
  • 1,538
  • 1
  • 11
  • 15