Rails 3 - 属于:唯一性
我想要建立一对一的关系,独一无二。
所以我在一个模型中有这个关联和验证
belongs_to :bicycle 验证:自行车,:存在=> true, :唯一性=> true
并给我这个错误:
ActiveRecord::StatementInvalid: SQLite3::SQLException: close "FROM": 语法错误: SELECT FROM "transactions" WHERE ("transactions"."bicycle" IS NULL) AND ("transactions"."bicycle " IS NULL) LIMIT 1
我不知道为什么查询构造得不好......这是rails3的一个错误?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能应该验证属性而不是关联。本例中的属性是 bike_id。因此,如果您将其更改为:
那应该可行。
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:
that should work.