自定义 rails/mongoid 关系名称不起作用

编程入门 行业动态 更新时间:2024-10-27 05:24:16
本文介绍了自定义 rails/mongoid 关系名称不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试让自定义关系名称在 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 时,我得到了零.mongoid/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. mongoid/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 关系名称不起作用

本文发布于:2023-11-22 19:04:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1618637.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   不起作用   名称   关系   mongoid

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!