mongoidreferences_and_referenced_in_many

发布于 2024-10-26 20:56:05 字数 1549 浏览 0 评论 0 原文

我有一个奇怪的问题。我是 mongoid 的新手,所以我很难确定是我还是 mongoid 的错...

class User
  include Mongoid::Document
  embeds_one  :profile, :class_name => "UserProfile"
  references_and_referenced_in_many :roles
end

class UserProfile
  include Mongoid::Document
  embedded_in :user
end

class Role
  include Mongoid::Document
  references_and_referenced_in_many :users
end

当我创建实例时,呈现我的代码可能是最好的解释(减去字段/验证/等),并具有以下关联这些对象的像这样...

user = User.new :username => 'username',
                :email => '[email protected]',
                :password => 'password'
user.build_profile  :first_name => 'John',
                    :last_name => 'Doe',
                    :birthday => Date.new(1980, 1, 1)
user.roles << Role.new(:name => 'Administrator')
user.save

...我可以使用 User.firstuser 查看此用户

...我可以使用 User.first 查看该用户的个人资料。 first.profile 和 user.profile

...我可以使用 user.roles 查看角色,但我无法使用 <代码>User.first.roles。

另一个奇怪的事情是 user.roles.countUser.first.roles.count 都返回 0,即使当我查看 user.roles 时也是如此,它返回 [#]。 (User.first.roles 返回一个空数组)

这似乎是一个错误。

i have a strange problem. i am new to mongoid, so i am having trouble determining if it is me or mongoid at fault... presenting my code is perhaps the best explanation (minus the fields/validations/etc.)

class User
  include Mongoid::Document
  embeds_one  :profile, :class_name => "UserProfile"
  references_and_referenced_in_many :roles
end

class UserProfile
  include Mongoid::Document
  embedded_in :user
end

class Role
  include Mongoid::Document
  references_and_referenced_in_many :users
end

with the following associations, when i create instances of these objects like so...

user = User.new :username => 'username',
                :email => '[email protected]',
                :password => 'password'
user.build_profile  :first_name => 'John',
                    :last_name => 'Doe',
                    :birthday => Date.new(1980, 1, 1)
user.roles << Role.new(:name => 'Administrator')
user.save

...i can view this user with User.first or user

...i can view the profile with User.first.profile and user.profile

...i can view roles with user.roles but i CANNOT view them with User.first.roles.

another strange thing is user.roles.count AND User.first.roles.count both return 0, even though when i view user.roles, it returns [#<Role _id: 4d8c0173e1607cdeae00002c, name: "Administrator", user_ids: [BSON::ObjectId('4d8c0173e1607cdeae00002a')]>]. (User.first.roles returns an empty array)

this seems like a bug.

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

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

发布评论

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

评论(1

浮云落日 2024-11-02 20:56:05

使用:自动保存=>对于关系关联为 true

references_and_referenced_in_many :roles, :autosave => true

,或者您可以显式将 role 保存为

role = Role.new(:name => 'Administrator')
user.roles << role
role.save
user.save

这是由于 mongoid.2.0.0.rc.1 + 列出的 此处

不再有关系关联
当父关系为时自动保存
创建的。以前保存在新的
具有references_many的文档
或引用_one 关联已加载
首先会挽救关系
节省。为了得到这个
功能返回,一个 :autosave =>
true 选项必须提供给
宏(这只适用于
参考文献_许多和参考文献_一个)

use :autosave => true for the relational association

references_and_referenced_in_many :roles, :autosave => true

or you can explicitly save the role as

role = Role.new(:name => 'Administrator')
user.roles << role
role.save
user.save

This is due to changes in mongoid.2.0.0.rc.1 + listed here.

Relational associations no longer
autosave when the parent relation is
created. Previously a save on a new
document which had a references_many
or references_one association loaded
would save the relations on it's first
save. In order to get this
functionality back, an :autosave =>
true option must be provided to the
macro (This only applies to
references_many and references_one)

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