ユーザーと勤務表の間に多対多の関連付けを設定しました。特定の勤務表の作成者も保存しようとしています。
クラス ユーザー
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
勤務表が作成されると、勤務表を作成したユーザーのIDを持つようにrotazationテーブルのcreator_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 の作業を達成する方法はありますか? ありがとう!