嵌套属性和通过控制器添加属性?

pja*_*mer 3 ruby-on-rails nested-attributes

由于这个问题很难描述,这是我能提出的最佳标题,所以这里有一些代码.

鉴于三个模型Parent,Child && Grandchild.

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
Run Code Online (Sandbox Code Playgroud)

我想将current_user.id添加到在Parent#new期间创建的子记录和Grandchild记录中.我现在使用隐藏字段,因为我找不到添加它们的好方法.

也许有人可以通过创建回调来在创建时添加current_user.id来提供帮助?无论如何,我从来没有太多运气进入模型,但你很聪明.

思考?

Joh*_*and 5

嗯,首先,我建议has_many :through从父母到孙子(通过孩子)的关系,反之亦然.有关更多详细信息,请参阅ActiveRecord关联类方法API中的"关联连接模型"部分.

至于你的主要问题,就像你说的那样,回调可能就是你想要的.我认为这样的事情应该这样做(虽然这是未经测试的代码):

class Parent
  # ...somewhere at the top...
  before_create :set_current_user_on_descendants

  # ...somewhere in the main class body...
  # (I assume parent['current_user'] is passed in as a typical 
  # parameter, and thus self.current_user is already set.)
  def set_current_user_on_descendants
    children.each { |c| c.current_user = self.current_user }
    grandchildren.each { |gc| gc.current_user = self.current_user }
  end
end
Run Code Online (Sandbox Code Playgroud)

有几个风格点可以做不同的.例如,你可以定义一个"后代"方法返回子孙子孙并迭代它,或者你可以在子孙类上实现回调(在这种情况下你可能想把它拉出来一个模块最大化) DRYness,虽然只有两个类中的单行方法可能是矫枉过正的).根据您想要更新current_user的确切时间,您可能希望使用before_save或其他一些回调而不是before_create- 您可以在ActiveRecord回调API中找到可用回调的完整列表.