Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rails 3 - belongs_to :uniqueness

I want do a one-to-one relationship, unique.

So I have in a model this association and validation

belongs_to :bicycle validates :bicycle, :presence => true, :uniqueness => true

And give me this error:

ActiveRecord::StatementInvalid: SQLite3::SQLException: near "FROM": syntax error: SELECT FROM "transactions" WHERE ("transactions"."bicycle" IS NULL) AND ("transactions"."bicycle" IS NULL) LIMIT 1

I don't know why the query is not well constructed... It is a bug of rails3?

like image 211
joao Avatar asked Sep 20 '25 10:09

joao


1 Answers

You should probably validate the attribute and not the association. The attribute in this case would be bicycle_id. So if you change it to:

validates :bicycle_id, :presence => true, :uniqueness => true

that should work.

like image 72
DanneManne Avatar answered Sep 22 '25 11:09

DanneManne