I am trying to order my assets so that they are appearing in descending order based off of the number of users. This code works in Postgres, but it doesn't seem to be working in Ruby. Not sure what is wrong. Any help is appreciated. Thank you in advance.
def order_assets
@asset = Asset.select("assets.id, count(assets_users.asset_id) as the_count")
.joins("LEFT OUTER JOIN assets_users ON assets.id = assets_users.asset_id")
.group("assets.id")
.having("count(*) > 0")
.order("the_count")
end
I want all of the yellow'd assets to be on the top, when the ones with users filled in below.

Postgres code:
SELECT
assets.id,
count(assets_users.asset_id) as the_count
FROM assets
LEFT OUTER JOIN assets_users ON assets.id = assets_users.asset_id
GROUP BY assets.id
HAVING count(*) > 0
ORDER BY the_count;
This is how the Postgres comes out:

.to_sqlto your ActiveRecord query? That method willreturn the SQL it creates. That might help you to find the differences betwen the query from Rails and the pure SQL query. Furthermore you write doesn't seem to be working, can you specify that?.to_sqlcomes out exactly as my sql code is typed out. And, the ruby code just doesn't seem to have any effect on the order of the assets.