我想摧毀一個嵌套模式,如果它的屬性形式父模型削除 - 然而,它看來,如果該模型是空白ActiveRecord::Callbacks
不叫。銷燬與空白嵌套屬性
class Artist < ActiveRecord::Base
using_access_control
attr_accessible :bio, :name, :tour_dates_attributes
has_many :tour_dates, :dependent => :destroy
accepts_nested_attributes_for :tour_dates, :reject_if => lambda { |a| a[:when].blank? || a[:where].blank? }, :allow_destroy => true
validates :bio, :name :presence => true
def to_param
name
end
end
和
class TourDate < ActiveRecord::Base
validates :address, :when, :where, :artist_id, :presence => true
attr_accessible :address, :artist_id, :when, :where
belongs_to :artist
before_save :destroy_if_blank
private
def destroy_if_blank
logger.info "destroy_if_blank called"
end
end
我有它使用fields_for
,以示對這位藝術家的相關巡演的領域,這適用於編輯和添加新的巡演藝術家形式,但如果我只是空白出遊日期(刪除它),destroy_if_blank
永遠不會被調用。可能藝術家控制器的@artist.update_attributes(params[:artist])
行不考慮值得更新的空白實體。
我錯過了什麼嗎?有沒有解決的辦法?
爲什麼有'tour_date.when.blank?兩次?謝謝 –
@maxkaplan:應該是'when'和'where'。我將它固定在答案中。謝謝! – Sunny