问题描述
我正在尝试让自定义关系名称在 Mongo 中工作.
I'm trying to get custom relationship names to work in Mongo.
拼贴"是一个 BSON 文档,其中填充了属于工作 ID 的 BSON 字段.以下是拼贴"数据库中的内容:
A "collage" is a BSON document filled with BSON fields that are Work Ids. Here's what's in the database for "collage":
Collage.create(slide_one: client.work.first.id, slide_two: client.work.second.id, slide_three: client.work.third.id)
因此,拼贴画是充满工作 ID 的 mongo 记录.我希望能够写@collage.work_one 或@collage.slide_one.work_one,或@collage.slide_one.work 来完成我想要的工作.
So, a collage is mongo record full of work Ids. I'd like to be able to write @collage.work_one or @collage.slide_one.work_one, or @collage.slide_one.work to get to the Work I want.
自定义命名这些关联被证明是徒劳的.到目前为止,我已经尝试了两件事:
Custom naming these associations is proving fruitless. So far I've tried two things:
这似乎是在 mongo 网站上说的,但是当我设置它并调用 @collage.work_one.inspect 时,我得到了零.http://mongoid.org/docs/relations.html(页面底部)
This is how it seems to say to do it on the mongo website, but when I set it up and call @collage.work_one.inspect I get nil. http://mongoid.org/docs/relations.html (bottom of page)
拼贴.rb
has_one :work_one, class_name: 'Work', inverse_of: :slide_one
工作.rb
belongs_to :slide_one, class_name: 'Collage', inverse_of: :work_one
@collage.work_one.inspect 只是打印出nil"
@collage.work_one.inspect literally just prints out "nil"
.
.
我也试过
拼贴.rb
has_one :work_one, class_name: 'Work', as: :work_oneable
工作.rb
belongs_to :work_oneable
但这给了我:
uninitialized constant WorkOneable
非常感谢任何帮助或想法!
Any help or ideas much appreciated!
推荐答案
我相信你的类声明是正确的(第一个).但是,您应该在分配字段时使用实际对象而不是 id,如下所示:
I believe your class declaration is correct (the first one). However, you should use the actual objects instead of id when assigning the fields, like this:
Collage.create(slide_one: client.work.first, slide_two: client.work.second, slide_three: client.work.third)
希望有所帮助.
这篇关于自定义 rails/mongoid 关系名称不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!