0

我有以下情況:嵌套屬性reject_if模型有四個以上孩子

class Question < ActiveRecord::Base 
    has_many :answers, :dependent => :destroy 
    accepts_nested_attributes_for :answers, :allow_destroy => true, :reject_if => "" 

end 

class Answer < ActiveRecord::Base 
    belongs_to :question 
end 

我怎麼能拒絕創作如果問題有4個以上的答案?

+1

我相信'accept_nested_attributes_for:answers,:allow_destroy => true,:reject_if => - > {| q | q ['answers']。count> 4}'應該這樣做 – CWitty

回答

3

我希望它適合你。

class Question < ActiveRecord::Base 
    has_many :answers, :dependent => :destroy 
    accepts_nested_attributes_for :answers, :reject_if => -> {|q| q['answers'].count > 4}, :allow_destroy => true 
end 

class Answer < ActiveRecord::Base 
belongs_to :question 
end