validate presence of has_and_belongs_to_many
has-and-belongs-to-many, ruby-on-rails
Solution
validate :require_at_least_one_kind
validate :limit_to_three_kinds
private
def require_at_least_one_kind
if kinds.count == 0
errors.add_to_base "Please select at least one kind"
end
end
def limit_to_three_kinds
if kinds.count > 3
errors.add_to_base "No more than 3 kinds, please"
end
end
Problem
Hi i'm using a has_and_belongs_to_many in a model. I want set the valitor of presence for kinds. and set the max number of kinds per core to 3 ``` class Core < ActiveRecord::Base has_and_belongs_to_many :kinds, :foreign_key => 'core_id', :association_foreign_key => 'kind_id' end ``` how can i do? thanks