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 に答える