Rails 3:“accepts_nested_attributes_for:reject_if”不起作用

时间:2011-08-13 17:55:45

标签: ruby-on-rails

我的UserQuestion模型有许多accepted_answers并接受以下嵌套属性:accepted_answers

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

我的表单发送以下参数:

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

我想我的问题是,lambda设置不正确,因为即使他们的answer_id为0,也会创建已接受的答案

1 个答案:

答案 0 :(得分:8)

试试这个:

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