我知道,你可以有:
accepts_nested_attributes_for :foo, :reject_if => proc { |a| a[:bar].blank? }
有没有一种方法,而不是说像
accepts_nested_attributes_for :foo, :reject_if => blah[:bar].blank? and flah[:bar].blank?
要么
accepts_nested_attributes_for :foo, :reject_if => all fields except record_date.blank?
谢谢
我对这个有点晚了,但你可以这样做:
accepts_nested_attributes_for :foo,
reject_if: ->(attributes){
attributes.except(:key).values.all?( &:blank? )
}
受此启发: https://rails.lighthouseapp.com/projects/8994/tickets/2501-any_blank-and-all_blank-options-for-accepts_nested_attributes_for-reject_if
这种精细工作对我来说:
reject_if: proc { |attributes| attributes.all? {|k,v| v.blank? || ['foo', 'bar', 'baz'].include?(k)} }
您可以替换['foo', 'bar', 'baz'].include?(k)
通过k == 'foo'
,如果只有一个例外,但第一语法使得PROC准备好多个的。
文章来源: Can reject_if be used to reject a nested resource if all fields except one is blank?