5

我知道你可以拥有:

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?

谢谢

4

2 回答 2

9

我在这方面有点晚了,但你可以这样做:

accepts_nested_attributes_for :foo, 
                               reject_if: ->(attributes){ 
                                 attributes.except(:key).values.all?( &:blank? ) 
                               }
于 2013-03-29T14:38:30.833 回答
0

受此启发: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)为,但第一种语法使 proc 为多个异常做好了准备。k == 'foo'

于 2012-08-29T13:06:03.807 回答