奇怪的 ActiveRecord 平等问题

发布于 2024-12-13 21:37:15 字数 940 浏览 2 评论 0原文

我已经通过覆盖 == 运算符相当简单地为我的实际应用程序修复了这个问题,但这让我发疯,我无法找到解释。据我所知,ActiveRecord 应该仅通过 id 字段来确定两个现有记录的相等性,对吧?

显然,不!

Loading development environment (Rails 3.0.4)
irb(main):001:0> c = ChallengeClaim.find(1)
=> #<ChallengeClaim id: 1, collection_id: 954, creation_id: nil, creation_type: nil, request_signup_id: 2, request_prompt_id: 5, claiming_user_id: 8, sent_at: nil, fulfilled_at: nil, defaulted_at: nil, created_at: "2011-09-23 04:39:07", updated_at: "2011-09-23 04:39:07">
irb(main):002:0> c2 = ChallengeClaim.find(2)
=> #<ChallengeClaim id: 2, collection_id: 954, creation_id: nil, creation_type: nil, request_signup_id: 2, request_prompt_id: 4, claiming_user_id: 8, sent_at: nil, fulfilled_at: nil, defaulted_at: nil, created_at: "2011-11-07 17:47:33", updated_at: "2011-11-07 17:47:33">
irb(main):003:0> c == c2
=> true

?!?!?!

非常欢迎任何解释,这样我晚上就可以再次入睡了。 :>

I've fixed this for my actual app fairly trivially by overriding the == operator, but it is driving me nuts and I haven't been able to find an explanation. As far as I know, ActiveRecord is supposed to determine the equality of two existing records just via the id field, right?

Apparently, no!

Loading development environment (Rails 3.0.4)
irb(main):001:0> c = ChallengeClaim.find(1)
=> #<ChallengeClaim id: 1, collection_id: 954, creation_id: nil, creation_type: nil, request_signup_id: 2, request_prompt_id: 5, claiming_user_id: 8, sent_at: nil, fulfilled_at: nil, defaulted_at: nil, created_at: "2011-09-23 04:39:07", updated_at: "2011-09-23 04:39:07">
irb(main):002:0> c2 = ChallengeClaim.find(2)
=> #<ChallengeClaim id: 2, collection_id: 954, creation_id: nil, creation_type: nil, request_signup_id: 2, request_prompt_id: 4, claiming_user_id: 8, sent_at: nil, fulfilled_at: nil, defaulted_at: nil, created_at: "2011-11-07 17:47:33", updated_at: "2011-11-07 17:47:33">
irb(main):003:0> c == c2
=> true

?!?!!??!

Any explanations gratefully welcomed so I can sleep at night again. :>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

檐上三寸雪 2024-12-20 21:37:15

正如 Chris Heald 在上面的评论中指出的那样,比较运算符 <=> 确实是这样的。是在 ChallengeClaim 上定义的!

As Chris Heald pointed out above in the comments, indeed the comparison operator <=> was defined on ChallengeClaim!

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