absolutskyy
absolutskyy

Reputation: 567

Rails how to reject_if when accepts_nested_attributes_for

In my Location model I have the following..

has_many :location_polls

and in the LocationPoll model, I have the following..

has_many :poll_locations

Now back in the Location model, I want to reject a location poll in case there are no poll locations. How can I do this? I tried the following and it did not work..

accepts_nested_attributes_for :location_polls, :reject_if => lambda { |a| a[:poll_locations].empty? }

My apologies if I'm missing information to help you answer the question. I didn't want to clutter up the question. Please let me know if more info is needed. Thanks.

Upvotes: 0

Views: 3746

Answers (1)

rocket scientist
rocket scientist

Reputation: 2447

If you are looking to ignore any blank :location_polls you can use :all_blank which will create a proc that will reject a record where all the attributes are blank excluding any _destroy values:

accepts_nested_attributes_for :location_polls, :reject_if => :all_blank

Upvotes: 2

Related Questions