MongoDB 中引用的奇怪行为

发布于 2024-10-19 19:28:52 字数 585 浏览 1 评论 0原文

我正在使用 Rails 3 和 Mongoid。

我有两个文档:

class MyUser
  include Mongoid::Document

  field ......

  references_many :statuses, :class_name => "MyStatus"
end

class MyStatus
  include Mongoid::Document

  field ......

  referenced_in :user, :class_name => "MyUser"
end

问题是,我可以获取任何给定状态的用户,但我无法获取用户的状态列表!

IE。

status = MyStatus.first
status.user # the output is correct here

user = MyUser.first
user.statuses # this one outputs [] instead of the list of statuses...

请告诉我我做错了什么?我刚接触 mongo 几天......

I am using Rails 3 with Mongoid.

I have two documents:

class MyUser
  include Mongoid::Document

  field ......

  references_many :statuses, :class_name => "MyStatus"
end

class MyStatus
  include Mongoid::Document

  field ......

  referenced_in :user, :class_name => "MyUser"
end

The problem is, I can get the user of any given status, but I cannot get the list of statuses from a user!

ie.

status = MyStatus.first
status.user # the output is correct here

user = MyUser.first
user.statuses # this one outputs [] instead of the list of statuses...

Please tell me what have I done wrong? I am just a few days with mongo......

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

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

发布评论

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

评论(1

两个我 2024-10-26 19:28:52

你的代码对我来说看起来是正确的。

您确定 MyStatus.first.user == MyUser.first 吗?

您的数据库中可能有多个用户..其中第一个用户没有状态,第二个用户的列表中有 status1 。

要测试这一点,请尝试执行以下操作:

status = MyStatus.first
user = status.user 
user.statuses         # Should return at least one status

Your code looks correct to me.

Are you sure that MyStatus.first.user == MyUser.first ?

It's possible that you have multiple users in your db.. where the first user has no statuses, and the second user has status1 in his list.

To test this, try doing:

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