验证rails 3.2中创建的has_many对象的数量?
我需要对关联对象的"最大/最小"计数进行自定义验证.我有房地产,有
has_many :realty_images, :dependent => :destroy
accepts_nested_attributes_for :realty_images
Run Code Online (Sandbox Code Playgroud)
和realty_image:
class RealtyImage < ActiveRecord::Base
attr_accessible :avatar, :image, :realty_id
belongs_to :realty
#here a suppose I need to put some kind of custom validation
mount_uploader :image, ImageUploader
end
Run Code Online (Sandbox Code Playgroud)
标准验证方法适用于关联:
class Ad
has_many :realty_images
# make sure there are some images
validates_presence_of :realty_images
# or make sure the number of images is in certain range
validates_length_of :realty_images, within: 5..10
end
Run Code Online (Sandbox Code Playgroud)
查看文档以获取更多详细信息.
| 归档时间: |
|
| 查看次数: |
784 次 |
| 最近记录: |