查找属于另一个集合的集合

发布于 2024-09-26 04:47:34 字数 95 浏览 0 评论 0原文

我确信对此有一个简单的答案,但不确定如何措辞来搜索它。

鉴于文章属于用户,如果我有一组三个不同的用户,我如何通过一个查询访问这些用户中的任何一个所写的所有文章?

I'm sure there's an easy answer for this, but not sure how to word it to search for it.

Given that articles belong to users, if I have a set of three different users, how can I access all of the articles written by any of those users with one query?

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

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

发布评论

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

评论(2

少跟Wǒ拽 2024-10-03 04:47:34

鉴于 user_ids 是用户 id 的数组:

Article.all(:joins => :users, :conditions => ["users.id in ?", user_ids])

Given that user_ids is an array of the user ids:

Article.all(:joins => :users, :conditions => ["users.id in ?", user_ids])
不交电费瞎发啥光 2024-10-03 04:47:34

hellvinz 的回答帮助我解决了这个问题:) 由于这些文章属于用户,这意味着它们具有 user_id 属性。我现在觉得这个很愚蠢 xD

user_ids = users.map(&:id)
articles = Article.all :conditions => {:user_id => user_ids}

保存一个连接,生成一个更简单的查询。

谢谢!

hellvinz's answer helped me work it out :) Since the articles belong to users, that means that they have the user_id attribute on them. I feel pretty silly about this one now xD

user_ids = users.map(&:id)
articles = Article.all :conditions => {:user_id => user_ids}

Saves a join, producing a simpler query.

Thanks!

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