我有三个模型。销售,物品和图像。我想验证,创建销售时,每个销售和一个或多个项目至少有三张照片。什么是实现这一目标的最佳方式?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