Reputation: 1996
I have a users table and a reporting_events table where reporting_events belongs to user.
I want to sort the users table based on the reporting events values. Users should be sortable based on values of various reporting event names.
For example, there can be reporting events of two users with reporting event name as "avg_response_time" and reporting event values as 1 and 2 for the two users. We should be able to sort with avg_response_time of user both in desc and asc orders.
What I have tried:
reports = users.joins(:reporting_events).where("reporting_events.name='avg_response_time'").order("reporting_events.value desc")
filter_params[:sort_order]
gives the column and direction it should sort by. When I don't use distict I'm getting duplicate users in users list.
I want to list all users even if reporting_events does't exist for the user.
Upvotes: 0
Views: 69
Reputation: 24350
You need a user an outer join
instead on an inner join
(default) :
users.joins('LEFT OUTER JOIN reporting_events ON reporting_events.user_id = users.id').where(...
Upvotes: 1