加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 没有为关联的集合对象调用Rails 3.0.10 before

发布时间:2020-12-17 02:42:36 所属栏目:百科 来源:网络整理
导读:我有一个名为Parent的对象has_many Child对象: has_many :childrenaccepts_nested_attributes_for :children,:allow_destroy = true Child包含一个指定:before_validation回调的模块: def self.included base base.class_eval do before_validation :my_c
我有一个名为Parent的对象has_many Child对象:

has_many :children
accepts_nested_attributes_for :children,:allow_destroy => true

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

我注意到,在为子项创建Parent和嵌套属性时,不会为每个Child调用:before_validation回调.这是预期的行为吗?我试过做一个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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读