需要有关 MongoID 和多对多关系的帮助

发布于 2024-10-19 14:39:07 字数 1235 浏览 0 评论 0原文

使用 mongoid 2.0.0.beta.20,如果有的话。

目标很简单,但我却达不到。我有一个 User 模型,其中

references_many :worlds, :inverse_of => :users

还有一个 World,当然,

references_many :users, :inverse_of => :worlds

用人类的话来说,用户可以访问 0..inf 世界,并且可以通过1..inf 用户。世界应该存储去过那里的用户的 id 列表,用户应该存储他访问过的世界的 id 列表。看起来很容易,但不想为我工作。

现在,有一个 User 实例(我们称他为 someone)和两个 World 实例(可能是 earthmars)。当我尝试说某人访问了地球时,没有问题:

earth.users << someone
earth.users.count # => 1

然后,我想说某人也去过mars

mars.users << someone
mars.users.count # => 1

一切都好吗?不确定:

earth.users.count # => 0
someone.worlds.count # => 0

我也尝试使用 :stored_as => :array 关系参数,但根本没有成功(方向无关紧要):(

someone.worlds << earth
NoMethodError: You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.<<

以及任何其他 Array 方法引发我试图处理 nil)

也许我错过了文档中的某些内容或做错了什么。请帮我解决这个问题。

Using mongoid 2.0.0.beta.20, if it means.

The goal is quite easy, but I can't get it. I have a User model, which

references_many :worlds, :inverse_of => :users

and a World, which, of course,

references_many :users, :inverse_of => :worlds

In human words, user can visit 0..inf worlds, and a world can be visited by 1..inf users. World should store a list of ids of users who been there, and user should store a list of worlds' ids he visited. Looks pretty easy, but doesn't want to work for me.

Now, there is an instance of User (let's call him someone) and two instances of World (earth and mars maybe). When I try to say that someone visited earth, there is no problem:

earth.users << someone
earth.users.count # => 1

Then, I want to say that someone also been on mars:

mars.users << someone
mars.users.count # => 1

Everything's OK? Not sure:

earth.users.count # => 0
someone.worlds.count # => 0

I also tried to use :stored_as => :array parameter for relations, but there was no success at all (direction doesn't matter):

someone.worlds << earth
NoMethodError: You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.<<

(and any other Array methods raises that I trying to deal with nil)

Maybe I missed something in the docs or doing something wrong. Please, help me to solve this problem.

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

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

发布评论

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

评论(2

一个人练习一个人 2024-10-26 14:39:07

如果您升级到最新的 rc,那么您可以使用新的宏来执行这些类型的关系:

references_and_referenced_in_many :worlds

在 beta 20 中,您使用 :stored_as => 走在正确的轨道上。 :大批。尝试像这样设置默认值,它应该对您有用。

References_many :worlds, :stored_as => :数组,:默认=> []

If you upgrade to the latest rc, then you can use the new macro for doing those kinds of relations:

references_and_referenced_in_many :worlds

In beta 20, you were on the right track using :stored_as => :array. Trying setting a default like so and it should work for you.

references_many :worlds, :stored_as => :array, :default => []

好听的两个字的网名 2024-10-26 14:39:07

对于 MongoID,我相信您可能必须在使用 earth.users << 等修改文档后显式 earth.save 文档。某人为了保留对“文档”的更改。

With MongoID I believe you may have to explicitly earth.save the document after modifying it with something like earth.users << someone in order to persist changes to the 'document'.

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