Bruno Miranda's Notebook

Personal Blog about Ruby on Rails, XHTML, CSS, and Design

If you a trying to validate if the value of an attribute is unique on the database, you can easily accomplish this by using the built in helper method…

validates_uniqueness_of
But how would you go about making sure the attribute value is unique based on a scope parameter? Let’s say you are building a holidays table and need to make sure you only allow one ‘Christmas’ holiday to be created per year:
  validates_uniqueness_of :name, 
:scope => :year, :message => "must be unique"

Hope that helps!