Rails accepts_nested_attributes count validation

寵の児 提交于 2019-12-01 20:49:23

问题


I've got three models. Sales, items, and images. I'd like to validate that when a sale is created there are at least three photos per sale and one or more items. What would be the best way to achieve this?

Sales Model:

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

Items Model:

class Item < ActiveRecord::Base

  belongs_to :sale, :dependent => :destroy
  has_many :images, :dependent => :destroy

  accepts_nested_attributes_for :images

end

Images Model:

class Image < ActiveRecord::Base
  belongs_to :item, :dependent => :destroy
end

回答1:


Create custom methods for validating

In your sales model add something like this:

validate :validate_item_count, :validate_image_count

def validate_item_count
  if self.items.size < 1
    errors.add(:items, "Need 1 or more items")
  end
end

def validate_image_count
  if self.items.images.size < 3
    errors.add(:images, "Need at least 3 images")
  end
end

Hope this helps at all, good luck and happy coding.




回答2:


Another option is using this little trick with the length validation. Although most examples show it being used with text, it will check the length of associations as well:

class Sale < ActiveRecord::Base
   has_many :items,  dependent: :destroy
   has_many :images, through: :items

   validates :items,  length: { minimum: 1, too_short: "%{count} item minimum" }
   validates :images, length: { minimum: 3, too_short: "%{count} image minimum" }
end

You just need to provide your own message as the default message mentions a character count.



来源:https://stackoverflow.com/questions/9932445/rails-accepts-nested-attributes-count-validation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!