Since this question is hard to describe, the best headline I could come up with, so here is some code.
Given the three models of Parent, Child && Grandson.
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
accepts_nested_attributes_for :grandchild
end
Grandchild < ActiveRecord::Base
belongs_to :parent
belongs_to :child
end
I want to add current_user.id to both the child record and the Grandchild record that is created during Parent # new. I used hidden fields because I could not find a good way to add them.
Can someone help by creating a callback to add current_user.id to create? In any case, I never managed to get this in the model, but you are smart.
Thoughts?
source
share