I have a model called Post (table posts), which has a relationship with a model called User (table users), which is referenced as "uploader" in the Post model (the posts table has a column called uploader_id which is the id of the table users). An User can have many Posts, some of which can be deleted (marked through the is_deleted boolean column in posts).
How do I get a list of users with the number of total and deleted posts?
::Post
.select(
Arel.star.count.as("total"),
Arel::Nodes::Case.new.when(Post.arel_table[:is_deleted]).then(1).sum.as("deleted_count"),
:uploader
)
.joins(:uploader)
.group(:uploader)
.order(:total, :deleted_count)
.limit(20)
This does not work. I need the user to be returned as model, the uploader_id is not sufficient.
I've tried many attempts at this but no matter what I do I can't figure it out. Either it tries to insert "#<Arel::Nodes::Count:0x0000565..." as a literal in the SQL string, or it returns arrays of nils.
What's extremely frustrating to me is that this works instead, if I only want the total posts and the user:
::Post
.joins(:uploader)
.group(:uploader)
.order(Arel.sql("count(*) desc"))
.limit(20)
.count
I just can't figure out how to add the conditional case sum, I'm a novice at rails and the documentation does not help.