【问题标题】:Is there a problem with my mongoid reference macros?我的 mongoid 参考宏有问题吗?
【发布时间】:2011-03-16 00:01:46
【问题描述】:

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

非常感谢您提供的任何帮助 - 让我头疼。谢谢!

【问题讨论】:

    标签: mongodb mongoid


    【解决方案1】:

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

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

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

    【讨论】:

    • 嗨,保罗!是的,当我运行=> u.questions_about.map 时,查询会查找用户字段,而不是 about_user 字段。知道为什么吗?
    【解决方案2】:

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

    升级到最新版本,一切正常

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多