我有複雜的模型/表單。我不想重複記錄,所以我想合併具有相似屬性的記錄。我將如何使用before_save回調取消「保存」?這是我在想什麼:Rails 4:如何取消「before_save」回調中的保存?
class ProductsColor < ActiveRecord::Base
before_save :check_for_similar_record
def check_for_similar_record
if ProductsColor.exist?(color_id: self.color_id, product_id: self.product_id)
# merge values with existing ProductsColor and stop self from saving
end
end
end
如果您返回nil,該記錄仍將保存,您必須顯式返回false才能取消保存。 – dezman
對於Rails 5來說,這已經不再是真實的 - 請參閱其他答案以獲取詳細信息。 –
@JonGjengset的問題是「Rails 4:如何取消」before_save「回調中的保存?」。我爲此得到了這個投票嗎? –