我有一个带有嵌套属性的表单。现在在我的:reject_if =>
语句中,我想检查嵌套模型上的一个属性,比如说first_record?
,有没有办法访问这样的方法?在我看来,您只能访问提交的属性散列,例如检查字段是否为空。谢谢!
发布于 2012-12-03 21:40:25
根据docs 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
这对你来说应该是可行的。基本上,这意味着在自定义函数中,你可以做任何你想做的事情。
https://stackoverflow.com/questions/13683948
复制相似问题