我有三個模型。銷售,物品和圖像。我想驗證,創建銷售時,每個銷售和一個或多個項目至少有三張照片。什麼是實現這一目標的最佳方式?Rails的accept_nested_attributes計數驗證
銷售模式:
class Sale < ActiveRecord::Base
has_many :items, :dependent => :destroy
has_many :images, :through => :items
accepts_nested_attributes_for :items, :reject_if => lambda { |a| a[:title].blank? }, :allow_destroy => true
end
產品型號:
class Item < ActiveRecord::Base
belongs_to :sale, :dependent => :destroy
has_many :images, :dependent => :destroy
accepts_nested_attributes_for :images
end
圖片型號:
class Image < ActiveRecord::Base
belongs_to :item, :dependent => :destroy
end
理想地命名這些方法validate_item_count和validate_image_count,因爲這會澄清您的意圖以及方法是否會添加錯誤。 – joelparkerhenderson 2012-03-29 21:17:41
好點,謝謝你的加入。 – digicazter 2012-03-29 21:27:11