嵌入参考未保存

发布于 2024-10-21 02:33:39 字数 1013 浏览 0 评论 0原文

在mongoid和rails 3下,我有一个用户集合和一个项目集合,其中嵌入了许多关系,模型是:

用户类
包括 Mongoid::文档
字段:名称,:类型=>字符串
Referenced_in:关系,:inverse_of => :用户
结束

类项目
包括 Mongoid::Document
字段:标题,:类型=>字符串
embeds_many :关系
结束

类关系
包括 Mongoid::Document
字段:类型,:类型=>字符串
references_one:用户
嵌入_in:主题,:inverse_of => :关系
结束

是关系的引用用户永远不会保存到关系中。例如,以下命令仅保存 :type

project1 = Project.new( :title => "project1", :relationships => [ {:type => "master", :user => "4d779568bcd7ac0899000002"} ] )

我的目标是拥有一个项目文档与此类似:

{ "_id" : ObjectId("4d77a8b2bcd7ac08da00000f"), "标题" : "项目1", "关系" : [
{
“类型”:“主控”,
“用户”:ObjectId(“4d775effbcd7ac05a8000002”),
“_id”:ObjectId(“4d77a8b2bcd7ac08da000010”)
}
] }

:user 从来不存在,我在这里遗漏了什么吗?非常感谢您的帮助!

特德

Under mongoid and rails 3 I have a collection of Users and a collection a Projects which embed many Relationships, the models are:

class User
include Mongoid::Document
field :name, :type => String
referenced_in :relationship, :inverse_of => :user
end

class Project
include Mongoid::Document
field :title, :type => String
embeds_many :relationships
end

class Relationship
include Mongoid::Document
field :type, :type => String
references_one :user
embedded_in :subject, :inverse_of => :relationships
end

My problem is that the referenced user of a relationship is never saved into the relationship. For example for following command only saves :type:

project1 = Project.new( :title => "project1", :relationships => [ {:type => "master", :user => "4d779568bcd7ac0899000002"} ] )

My goal is to have a project document similar to this:

{ "_id" : ObjectId("4d77a8b2bcd7ac08da00000f"), "title" : "project1", "relationships" : [
{
"type" : "master",
"user" : ObjectId("4d775effbcd7ac05a8000002"),
"_id" : ObjectId("4d77a8b2bcd7ac08da000010")
}
] }

The :user is never present, am I missing something here? Thanks a lot for your help!

Ted

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

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

发布评论

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

评论(1

迷迭香的记忆 2024-10-28 02:33:39

因此,您可能需要更改以下几件事:

1)避免使用字段名称“type”,因为这是单表继承使用的 Rails 魔术列名称。也许将它们更改为user_type和relationship_type。

2) 在 Mongoid 2.0 及更高版本中,您可以使用 Active Model 语法(例如 has_many 和 Belongs_to)来代替引用。 http://mongoid.org/docs/relations/referenced/1-n.html

3) 对于您的创建,不要为 user 分配用户 ID,而是尝试分配用户对象。

project1 = Project.new( :title => "project1", :relationships => [ {:type => "master", :user => User.first} ] )

或者您可以像这样分配 user_id:

project1 = Project.new( :title => "project1", :relationships => [ {:type => "master", :user_id => "the_use_id_you_want_to_associate"} ] )

仅供参考,您不必在“referenced_in :relationship, :inverse_of => :user”中指定 inverse_of。只需“referenced_in :relationship”就可以了。

So a couple things you might want to change:

1) Avoid the field name "type" as this is a rails magic column name used by single table inheritance. Maybe change them to user_type and relationship_type.

2) With Mongoid 2.0 and up you can use Active Model syntax like has_many and belongs_to instead of references. http://mongoid.org/docs/relations/referenced/1-n.html

3) For your create, instead of assigning user with a user ID, try assigning a user object.

project1 = Project.new( :title => "project1", :relationships => [ {:type => "master", :user => User.first} ] )

Or you could assign a user_id like so:

project1 = Project.new( :title => "project1", :relationships => [ {:type => "master", :user_id => "the_use_id_you_want_to_associate"} ] )

FYI, you don't have to specify the inverse_of in "referenced_in :relationship, :inverse_of => :user". Just "referenced_in :relationship" will do the trick.

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