Bra*_*ley 4 activerecord ruby-on-rails
我有一个名为Parent的对象has_many Child对象:
has_many :children
accepts_nested_attributes_for :children, :allow_destroy => true
Run Code Online (Sandbox Code Playgroud)
Child包含一个指定: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
Run Code Online (Sandbox Code Playgroud)
我注意到,在为子项创建Parent和嵌套属性时,:before_validation不会为每个Child调用回调.这是预期的行为吗?我试过做一个before_save回调,它似乎工作正常.
这是在Rails 3.0.10上.
谢谢!
你应该使用validates_associated:
class Parent < ActiveRecord::Base
has_many :children
accepts_nested_attributes_for :children, :allow_destroy => true
validates_associated :children
end
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1010 次 |
| 最近记录: |