Mexxer
Mexxer

Reputation: 1777

Rails 3: "accepts_nested_attributes_for :reject_if" doesn't work

My UserQuestion model has many accepted_answers and accepts nested attributes for :accepted_answers

    accepts_nested_attributes_for :accepted_answers, :reject_if => lambda { |a| ( a[:answer_id] == 0) }, :allow_destroy => true

My form sends the following parameters:

   "accepted_answers_attributes"=>{"0"=>{"answer_id"=>"0"}, "1"=>{"answer_id"=>"25"}, "2"=>{"answer_id"=>"0"}}

I guess my problem is, that the lambda isn't set up right, because accepted answers are created even though their answer_id is 0

Upvotes: 0

Views: 2716

Answers (1)

Vasiliy Ermolovich
Vasiliy Ermolovich

Reputation: 24617

try this:

:reject_if => lambda { |a| ( a[:answer_id].to_i == 0) }

Upvotes: 8

Related Questions