2013-06-25 79 views
1

這裏是(簡化)代碼:reject_if選項被忽略

class Biosimilar::AdverseEvent < ActiveRecord::Base 

    attr_accessibile :adverse_event_med_conds_attributes 

    has_many :adverse_event_med_conds, 
     :class_name => 'Biosimilar::AdverseEventMedCond', 
     :dependent => :destroy 
    has_many :med_conds, 
     :class_name => 'Biosimilar::MedCond', 
     :through => :adverse_event_med_conds 

    accepts_nested_attributes_for :adverse_event_med_conds, 
     :allow_destroy => true, 
     :reject_if => proc { |attributes| attributes.any? {|k,v| v.blank?} } 
end  

當提交表單時,該記錄上,即使用戶離開「med_cond_id」字段爲空「adverse_event_med_conds」表中創建。 reject_if不起作用!

有什麼建議嗎?

+0

如果您明確檢查該屬性會發生什麼?例如。 '':reject_if => proc {| attributes |屬性[ 'med_cond_id']。空白? }'' – GSP

回答

0

好吧,算了吧。上面的代碼是正確的!問題是在控制器中,我意外地「秀」的方法離開了下面幾行:

if @adverse_event.adverse_event_med_conds.size == 0 
    @adverse_event.adverse_event_med_conds.build 
end 

...和「建設」的號召是造成創紀錄的創造之一。