0

I have a Message and Source model related as follows:

class Message < ActiveRecord::Base
  has_many :sources
  accepts_nested_attributes_for :sources, :allow_destroy => true, :reject_if => proc{|s| s[:href].blank?}
end

class Source < ActiveRecord::Base
  belongs_to :outgoing_message
  validates_presence_of :href
end

When I submit my form (built using form_for and fields_for) it filters out any new sources with blank hrefs. But what I want is for it to delete any existing sources whose hrefs have been set to blank. Is there a simple way to do that?

4

1 回答 1

1

嗨,在您的Message模型内部,您可以添加validates_associated :sources 如果您需要在保存新记录之前从数据库中清除所有Message空白记录,那么您可以在控制器中编写:href

before_filter :some_filter, :only=>[:form_action]
...
def some_filter 
  Source.delete_all("href = '' OR href IS NULL")
end
于 2010-10-15T16:01:59.133 回答