ruby on rails - How can you validate a single primary record from a collection of has many records? -
in rails application have team model, , team has many members. member model has is_primary_contact boolean field. team can have 0 or 1 primary contact team members. i'm bit stuck on how go writing validation rule in order enforce restriction. should on team or member?
one way of doing simple validation on member model validates uniqueness of field. validate in event true, like
class member < activerecord::base validates_uniqueness_of :is_primary_contact, if: is_primary_contact? end i haven't tested code give go, should cover you're after :)
Comments
Post a Comment