
Untitled
By: a guest on
May 18th, 2012 | syntax:
None | size: 0.46 KB | hits: 19 | expires: Never
Rails HABTM checkbox validation
model Profile
has_and_belongs_to_many :browsers
end
model Browser
has_and_belongs_to_many :profiles
end
validate :must_have_at_least_one_browser
private
def must_have_at_least_one_browser
if browsers.blank?
errors.add(:base, 'You must select at least one browser')
end
end
def must_have_at_least_one_browser
errors.add(:base, 'You must select at least one browser') unless browser_ids.count > 0
end