我的 mongoid 参考宏有问题吗?

发布于 2024-10-22 01:08:45 字数 631 浏览 2 评论 0原文

Mongoid 正在执行的查询对我来说没有意义,我想我一定是定义了一些错误的东西。

但我不知道问题是什么

==Model Definition==
User
  references_many :questions_about, :class_name=>"Question", :inverse_of => :about_user

Question
  references_in :about_user, :class_name=>"User",:inverse_of => :questions_about

控制台:

u=User.find("nazroll")
u.questions_about.map
db['questions'].find({"user_id"=>#010b}, {})

应该执行的正确查询应该是

db['questions'].find({"ABOUT_user_id"=>#010b}, {})

这是因为我将 :questions_about 的逆定义为 :about_user

非常感谢提供的任何帮助 - 撕扯我的头。谢谢!

The query Mongoid is executing does not make sense to me, and I think I must be defining a couple of things wrong.

But I do not know what the problem is

==Model Definition==
User
  references_many :questions_about, :class_name=>"Question", :inverse_of => :about_user

Question
  references_in :about_user, :class_name=>"User",:inverse_of => :questions_about

Console:

u=User.find("nazroll")
u.questions_about.map
db['questions'].find({"user_id"=>#010b}, {})

The correct query that should have been executed should be

db['questions'].find({"ABOUT_user_id"=>#010b}, {})

This is because I am defining the inverse of the :questions_about as :about_user

Any help rendered is greatly appreciated - tearing my head out. Thanks!

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

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

发布评论

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

评论(2

他不在意 2024-10-29 01:08:45

在本例中,referenced_in 宏向您的对象添加一个名为 about_user_id 的字段,它是被引用的 UserBSON::ObjectIdreferences_many 宏只是使用您从中调用它的 User 实例中的 id 生成对该字段的查询。

换句话说,您可以将 referenced_in 视为 belongs_to,将 references_many 视为 has_many

您面临的问题到底是什么?它没有运行正确的查询吗?

The referenced_in macro, in this case, adds a field to your object called about_user_id and it is the BSON::ObjectId of the User being referenced. The references_many macro is simply going to generate a query for that field using the id in the User instance you call it from.

In other words, you can think of referenced_in as a belongs_to and references_many as a has_many.

What exactly is the problem you are facing? Is it not running the correct query?

分开我的手 2024-10-29 01:08:45

好吧,这实际上是因为我使用的是旧版本的 mongoid,它实际上有严重的错误。

升级到最新版本一切正常

Ok, it was actually because i was using an older version of mongoid, which was actually seriously buggy.

Upgrade to the latest version and all is fine

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