Rails 3 - 属于:唯一性

发布于 2024-10-06 05:39:47 字数 375 浏览 0 评论 0原文

我想要建立一对一的关系,独一无二。

所以我在一个模型中有这个关联和验证

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦太阳 2024-10-13 05:39:47

您可能应该验证属性而不是关联。本例中的属性是 bike_id。因此,如果您将其更改为:

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

那应该可行。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文