Inconsistent validate_uniqueness_of & :scope behavior?

(Rails 2.3.4)
Can’t quite figure out what I’m doing wrong but I’m seeing different
behavior for validates_uniqueness_of between two models that look more
ore less identical to me –

I have a model called Person:

class Person < ActiveRecord::Base
validates_uniqueness_of :first_name, :scope => :last_name

I also have a model called Setting (application settings object):

class Setting < ActiveRecord::Base
validates_uniqueness_of :setting, :scope => :category
{some class methods}

Here’s the schemas:

create_table “people”, :force => true do |t|
t.string “last_name”
t.string “first_name”
t.datetime “created_at”
t.datetime “updated_at”

create_table “settings”, :force => true do |t|
t.string “setting”
t.string “value”
t.float “num_value”
t.string “category”
t.string “tag”
t.datetime “created_at”
t.datetime “updated_at”

When I try to add a Person where two rows have the same first name, but
different last names, I’m able to save both records successfully.

However when I try to add a Setting where two rows have the same
‘setting’, but different ‘category’, I got an

ActiveRecord::RecordInvalid: Validation failed: Setting has already been

Interstingly, the executed SQL in the log file when checking enforcement
of the constraint seems to be different for the two models, although so
far as I can see, the structure is similar in all the relevant

SELECT FROM people WHERE (people.first_name = BINARY
‘jack’ AND people.last_name = ‘kramer’) LIMIT 1
whereas the check performed for dupes in setting is:

SELECT * FROM settings WHERE (settings.setting IN
(’settings.setting = BINARY ? AND settings.category =
?’,‘width’,‘frame’)) LIMIT 1

(Note that his returned “false” even though there is no Setting where
s.category = “frame”

What am I doing wrong? Why is genereated sql for People simply “where
a and b” with for Settings it is
where (a in (b and c))?


It may not be the problem, but I think there is some strangeness there
in that you have a model called Setting and an attribute called setting.

possibly, Rails is getting confused - is it validating uniqueness of
setting the attribute, or is it thinking that there can be only one
Setting instance period…?

Try changing either your model name or that ‘setting’ attribute name.
and see if that fixes it…

maybe you can call the attribute ‘name’