开发者

rails 3 - belongs_to :uniqueness

开发者 https://www.devze.com 2023-01-28 17:47 出处:网络
I want do a one-to-one relationship, unique. So I have in a model this association and validation开发者_如何学编程

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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消