我有一个带有嵌套属性的表单。现在在我的:reject_if =>
语句中,我想检查嵌套模型上的属性,比如说first_record?
有没有办法访问这样的方法?在我看来,您只能访问提交的属性哈希,以检查字段是否为空。谢谢!
答案 0 :(得分:5)
根据文档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
这对你有用。基本上这意味着在自定义功能中你可以做任何你想做的事情。