I have a Customer model that belongs_to Brand. Customer has only name and identifier (a string) as attributes. I want to enforce the uniqueness of name and identifier only within a particular brand. How can I enforce that scoped uniqueness?
Use the :scope
parameter of the ActiveRecord::Validations#validates_uniqueness_of
validation:
validates_uniqueness_of :brand_id, :scope => [:name, :identifier]
Alternately:
validates :brand_id, :uniqueness => {:scope => [:name, :identifer]}
Either way, this says, "for a given name
and identifier
, the brand_id
must be unique".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With