I am at a loss with this, and cannot figure it out for the life of me 😐
Below, @selected_posts represents a query on Posts. I want to query sort users that have posts in @selected_posts. Currently, with the code below I am returning a proper user list of ALL users in the users table. I would get Mike, 5 – Joe, 3 – John, 0 – Jack, 0 — the zeros show that it is querying every single user (which is going to be a problem. Any suggestions on how to fix the below code to get it to only query the users that have posts in @selected_posts?
@posts = user.includes(@selected_posts).map{ |user|
[user.name, user.posts.count, user.username]
}.sort
Thanks to any help, I am in debt to you (you have no idea how crazy this has been driving me)
As I undesrstood, you have models: Post and User. User can have many posts. And your goal is to get all user that have posts. You can do it in different ways. For example: define scope in User model