简体   繁体   中英

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

try this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM