根据关联中的字段对集合进行排序

发布于 2025-01-08 10:03:40 字数 500 浏览 2 评论 0原文

我在这里找不到如何使用 MongoDB/Mongoid 进行查询。所以我有一个模型 User,它 has_many :scoresScore 是另一个具有 code代码用户范围内是唯一的。对于给定的代码,我想获取按 scorevalue 排序的用户。

基本上,我想做的是: 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 技术交流群。

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

发布评论

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

评论(1

妖妓 2025-01-15 10:03:40

我想这可能就是你想要的:

scores = Score.where('code' => code).order_by('value')

如果你想获取用户(假设 Score 类有 belongs_to :user),那么你可以这样做:

users_that_matches_code = scores.map { |s| s.user }

I think this is probably what you want:

scores = Score.where('code' => code).order_by('value')

If you want to get the user (assuming that Score class has belongs_to :user), then you can do this:

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