Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can reject_if be used to reject a nested resource if all fields except one is blank?

I know that you can have:

accepts_nested_attributes_for :foo, :reject_if => proc { |a| a[:bar].blank? }

Is there a way to instead say something like

accepts_nested_attributes_for :foo, :reject_if => blah[:bar].blank? and flah[:bar].blank?

or

accepts_nested_attributes_for :foo, :reject_if => all fields except record_date.blank?

Thanks

like image 960
oort Avatar asked Jun 18 '12 17:06

oort


1 Answers

I'm a bit late on this, but you can do :

accepts_nested_attributes_for :foo, 
                               reject_if: ->(attributes){ 
                                 attributes.except(:key).values.all?( &:blank? ) 
                               }
like image 72
m_x Avatar answered Sep 28 '22 07:09

m_x