I have set up a many-to-many association between users and rotas. I am trying to also store who the creator of a particular rota is.
Class User
has_many :rotazations
has_many :rotas, :through => :rotazations
Class Rota
has_many :rotazations
has_many :users, :through => :rotazations, :order => 'name ASC'
Class Rotazation
belongs_to :user
belongs_to :rota
before_create do |rotazation|
rotazation.creator_id = rotazation.user_id
end
When a rota is created, I have successfully set the creator_id in the rotazation table to have the id of the user who created the rota. My question is, how do I access it:
rota.creator
My attempt
I have tried to add the following:
Rota Class
...
belongs_to :creator, :class_name => "User", :through => :rotazations, :foreign_key => "creator_id"
...
User Class
has_many :created_rotas, :class_name=> "Rota", :through => :rotazations, :foreign_key => "creator_id"
However, this doesnt work. Any ideas how to achieve rota.creator work? Thanks!