根据关联中的字段对集合进行排序
我在这里找不到如何使用 MongoDB/Mongoid 进行查询。所以我有一个模型 User
,它 has_many :scores
,Score
是另一个具有 code
和值
。 代码
在用户
范围内是唯一的。对于给定的代码,我想获取按 score
的 value
排序的用户。
基本上,我想做的是: User.where('scores.code' => code).order_by('scores.value')
,但不能这样做。我尝试了几件事,我认为答案与 User.where(:scores.matches => {:code => code})
有关,但这不会返回任何内容,所以我一定在这里遗漏了一些东西..
感谢您的时间,希望我说得足够清楚!
I'm having a trouble finding how to do a query with MongoDB/Mongoid here. So I have a model User
, that has_many :scores
, Score
being another model that has a code
and a value
. The code
is unique in the scope of the user
. For a given code I want to get the users, sorted by the value
of the score
.
Basically, what I want to do is something like : User.where('scores.code' => code).order_by('scores.value')
, except it cannot be done like this. I tried several things, and I think the answer is related to User.where(:scores.matches => {:code => code})
, but this does not return me anything, so I must be missing something here..
Thanks for your time, hope I was clear enough!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想这可能就是你想要的:
如果你想获取用户(假设 Score 类有
belongs_to :user
),那么你可以这样做:I think this is probably what you want:
If you want to get the user (assuming that Score class has
belongs_to :user
), then you can do this: