1

这是(简化的)代码:

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 不起作用!

有什么建议么?

4

1 回答 1

0

好吧,算了。上面的代码是正确的!问题出在控制器中,我不小心在“show”方法中留下了以下几行:

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

...并且“构建”调用是导致记录创建的调用。

于 2013-06-25T14:11:27.807 回答