nillkill
nillkill

Reputation: 11

Rails 3 validates_uniqueness_of with :scope generates invalid SQL?

Have two models that record some monthly statistics and I'm trying to enforce a composite key constraint within the model using:

validates_uniqueness_of :entity_id, :scope => [:year, :month]

When trying to run a .valid? method on a record, I continually get an error as apparently ActiveRecord is generating improper SQL.

SQLite3::SQLException: near "FROM": syntax error: SELECT      FROM       "table"  WHERE     ("table"."entity_id" = 1) AND ("table"."year" = 2007) AND ("table"."month" = 6) LIMIT 1

Notice above that Rails isn't adding a * in the select statement and so SQLite correctly throws an error.

Any ideas if I've done something wrong here?

Upvotes: 1

Views: 2074

Answers (1)

drhenner
drhenner

Reputation: 2230

Try this

validates :entity_id, :uniqueness => {:scope => [:year, :month]}

you probably want

:presence => true 

Upvotes: 2

Related Questions