8

我有一个带有嵌套属性的表单。现在在我的:reject_if =>声明中,我想检查嵌套模型上的一个属性,比如first_record?有没有办法访问这种方法?在我看来,您只能访问提交的属性哈希,例如检查字段是否为空白。谢谢!

4

1 回答 1

7

根据文档http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html

或者, :reject_if 也接受使用方法的符号:

class Member < ActiveRecord::Base
  has_many :posts
  accepts_nested_attributes_for :posts, :reject_if => :new_record?
end

class Member < ActiveRecord::Base
  has_many :posts
  accepts_nested_attributes_for :posts, :reject_if => :reject_posts

  def reject_posts(attributed)
    attributed['title'].blank?
  end
end

这应该适合你。基本上这意味着在自定义函数中你可以做任何你想做的事情。

于 2012-12-03T13:40:25.217 回答