我已经在用户和轮班之间建立了多对多的关联。我还试图存储特定轮换表的创建者是谁。
类用户
has_many :rotazations
has_many :rotas, :through => :rotazations
轮值班
has_many :rotazations
has_many :users, :through => :rotazations, :order => 'name ASC'
类轮换
belongs_to :user
belongs_to :rota
before_create do |rotazation|
rotazation.creator_id = rotazation.user_id
end
创建轮播表时,我已成功将轮播表中的 creator_id 设置为具有创建轮播表的用户的 id。我的问题是,我如何访问它:
rota.creator
我的尝试
我试图添加以下内容:
轮值班
...
belongs_to :creator, :class_name => "User", :through => :rotazations, :foreign_key => "creator_id"
...
用户类
has_many :created_rotas, :class_name=> "Rota", :through => :rotazations, :foreign_key => "creator_id"
但是,这不起作用。任何想法如何实现 rota.creator 工作?谢谢!