用户可以向代码段添加标记:
class Snippet < ActiveRecord::Base
# Relationships
has_many :taggings
has_many :tags, :through => :taggings
belongs_to :closing_reason
end
我想验证标签的数量:至少1个,最多6个.我要怎么做?谢谢
用户可以向代码段添加标记:
class Snippet < ActiveRecord::Base
# Relationships
has_many :taggings
has_many :tags, :through => :taggings
belongs_to :closing_reason
end
我想验证标签的数量:至少1个,最多6个.我要怎么做?谢谢
你可以创建一个custom validation.
差不多
validate :validate_tags
def validate_tags
errors.add(:tags, "too much") if tags.size > 5
end