この質問を説明するのは難しいので、それが私が思いつくことができる最高のタイトルなので、ここにいくつかのコードがあります.
親、子、孫の 3 つのモデルが与えられます。
Parent < ActiveRecord::Base
has_many :children
has_many :grandchildren
accepts_nested_attributes_for :child
end
Child < ActiveRecord::Base
belongs_to :parent
has_many :kids, :as => :grandchildren #this is just an example
accepts_nested_attributes_for :grandchild
end
Grandchild < ActiveRecord::Base
belongs_to :parent
belongs_to :child
end
Parent#new で作成される子レコードと孫レコードの両方に current_user.id を追加したいと思います。隠しフィールドを追加する良い方法が見つからなかったため、今のところ隠しフィールドを使用しています。
作成時に current_user.id を追加するコールバックを作成することで、誰かが助けてくれるでしょうか? とにかく、それをモデルに入れることはあまり運がありませんでしたが、あなたは頭がいいです。
考え?