我有一个名为 Parent 的对象,它有很多子对象:
has_many :children
accepts_nested_attributes_for :children, :allow_destroy => true
子级包含一个模块,该模块指定:before_validation
打回来:
def self.included base
base.class_eval do
before_validation :my_callback
end
end
protected
def my_callback
logger.debug "see me!"
end
我注意到,在为子级创建父级和嵌套属性时,:before_validation
不会为每个孩子调用回调。这是预期的行为吗?我尝试过做一个before_save
相反,它似乎工作正常。
这是在 Rails 3.0.10 上。
Thanks!
你应该使用validates_associated http://api.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html#method-i-validates_associated:
class Parent < ActiveRecord::Base
has_many :children
accepts_nested_attributes_for :children, :allow_destroy => true
validates_associated :children
end
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)