Ruby-on-rails rails where() sql 查询数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6900297/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
rails where() sql query on array
提问by stewart715
I'll explain this as best as possible. I have a query on user posts:
我会尽可能地解释这一点。我对用户帖子有疑问:
@selected_posts = Posts.where(:category => "Baseball")
@selected_posts = Posts.where(:category => "Baseball")
I would like to write the following statement. Here it is in pseudo terms:
我想写以下声明。这是伪术语:
User.where(user has a post in @selected_posts)
User.where(user has a post in @selected_posts)
Keep in mind that I have a many to many relationship setup so post.useris usable.
请记住,我有一个多对多的关系设置,所以post.user是可用的。
Any ideas?
有任何想法吗?
/EDIT
/编辑
@posts_matches = User.includes(@selected_posts).map{ |user|
[user.company_name, user.posts.count, user.username]
}.sort
Basically, I need the above to work so that it uses the users that HAVE posts in selected_posts and not EVERY user we have in our database.
基本上,我需要上述内容才能工作,以便它使用在 selected_posts 中发布帖子的用户,而不是我们数据库中的每个用户。
采纳答案by Andrew
Just use the following:
只需使用以下内容:
User.find(@selected_posts.map(&:user_id).uniq)
This takes the user ids from all the selected posts, turns them into an array, and removes any duplicates. Passing an array to user will just find all the users with matching ids. Problem solved.
这从所有选定的帖子中获取用户 ID,将它们转换为一个数组,并删除任何重复项。将数组传递给用户只会找到具有匹配 ID 的所有用户。问题解决了。
To combine this with what you showed in your question, you could write:
要将其与您在问题中显示的内容结合起来,您可以编写:
@posts_matches = User.find(@selected_posts.map(&:user_id).uniq).map{ |user|
[user.company_name, user.posts.size, user.username]
}
Use sizeto count a relation instead of countbecause Rails caches the size method and automatically won't look it up more than once. This is better for performance.
使用size计数的关系,而不是count由于Rails缓存大小的方法和自动不会看它不止一次。这对性能更好。
Not sure what you were trying to accomplish with Array#sort at the end of your query, but you could always do something like:
不确定您在查询结束时尝试使用 Array#sort 完成什么,但您始终可以执行以下操作:
@users_with_posts_in_selected = User.find(@selected_posts.map(&:user_id).uniq).order('username DESC')
回答by Harish Shetty
Try this:
尝试这个:
user.posts.where("posts.category = ?", "Baseball")
Edit 1:
编辑1:
user.posts.where("posts.id IN (?)", @selected_posts)
Edit 2:
编辑2:
User.select("users.company_name, count(posts.id) userpost_count, user.username").
joins(:posts).
where("posts.id IN (?)", @selected_posts).
order("users.company_name, userpost_count, user.username")
回答by jhlllnd
I don't understand your question but you can pass an array to the where method like this:
我不明白你的问题,但你可以像这样将数组传递给 where 方法:
where(:id => @selected_posts.map(&:id))
and it will create a SQL query like WHERE id IN (1,2,3,4)
它将创建一个 SQL 查询,如 WHERE id IN (1,2,3,4)
回答by Jeff Paquette
By virtue of your associations your selected posts already have the users:
凭借您的关联,您选择的帖子已经拥有以下用户:
@selected_posts = Posts.where("posts.category =?", "Baseball")
@users = @selected_posts.collect(&:user);
You'll probably want to remove duplicate users from @users.
您可能希望从@users 中删除重复的用户。

