我的 mongoid 参考宏有问题吗?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在本例中,referenced_in 宏向您的对象添加一个名为
about_user_id
的字段,它是被引用的User
的BSON::ObjectId
。references_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 theBSON::ObjectId
of theUser
being referenced. Thereferences_many
macro is simply going to generate a query for that field using theid
in theUser
instance you call it from.In other words, you can think of
referenced_in
as abelongs_to
andreferences_many
as ahas_many
.What exactly is the problem you are facing? Is it not running the correct query?
好吧,这实际上是因为我使用的是旧版本的 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