Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我收集了有帖子的用户,我想从所有用户那里获取所有帖子。有没有办法做这样的事情:
User.active.posts.unique.visible
Active 和 visible 都是我自己编写的特殊作用域。
假设您的表user_id中有列posts:
user_id
posts
Post.where(user_id: User.active.map(&:id)).visible
它将生成两个没有任何连接的 SQL 查询(除非您在visible范围内使用连接),因此这是一种非常有效的方法。
visible