Rails 3 Order by Count on Has_Many :Through

Rails 3 Order By Count on has_many :through

Tag.joins(:taggings).select('tags.*, count(tag_id) as "tag_count"').group(:tag_id).order(' tag_count desc')

try it desc, asc and see the difference.

We need select, to have tag_count column, and we need tag_count column to apply order to it, rest all straight forward join and grouping.

Btw, why dont you try this out https://github.com/mbleigh/acts-as-taggable-on

Order by count of has_many :through items in Rails 3

The most reasonable way to do this is to use a subquery written as raw SQL for ordering the result as follows...

Section.order(
'(select count(1) from votes inner join users on votes.user_id=users.id where users.section_id=sections.id)'
)

Order By Count on has_many :through

My rescues solution was to add a integer numberfollowers in my Project model

But with another project and the rails guides (http://guides.rubyonrails.org/active_record_querying.html), I finally found the answer to my request

Project.joins(:relationshipfollows).select('projects.*, COUNT(followed_id) as user_count').group('projects.id').order('user_count DESC')

Rails 3. Order by count of matches (many to many)

I guess you need to group by works, and order by count

That's how to sort from less works to more:

User.joins(:works).group("user_works.user_id").order("COUNT(*)") 

That's how to sort from more works to less:

User.joins(:works).group("user_works.user_id").order("COUNT(*) DESC") 

Upd.
If you want to have some extra filtering just add where clause

User.joins(:works).where("user_works.work_id in #{filter_string}").group("user_works.user_id").order("COUNT(*) DESC")

Rails order by results count of has_many association

If you expect to use this query frequently, I suggest you to use built-in counter_cache

# Job Model
class Job < ActiveRecord::Base
belongs_to :company, counter_cache: true
# ...
end

# add a migration
add_column :company, :jobs_count, :integer, default: 0

# Company model
class Company < ActiveRecord::Base
scope :featured, order('jobs_count DESC')
# ...
end

and then use it like

@featured_company = Company.featured

Rails 3 ActiveRecord: Order by count on association

Using named scopes:

class Song
has_many :listens
scope :top5,
select("songs.id, OTHER_ATTRS_YOU_NEED, count(listens.id) AS listens_count").
joins(:listens).
group("songs.id").
order("listens_count DESC").
limit(5)

Song.top5 # top 5 most listened songs

rails query by count on has_many relationship

Post.left_outer_joins(:comments)
.group(:id) # required by Postgres
.order(Arel.sql('COUNT(comments.*) DESC')) # https://github.com/rails/rails/issues/32995

If you want to use the count in the result you can select it as well:

Post.left_outer_joins(:comments)
.select('posts.*, COUNT(comments.*) AS comment_count')
.group(:id) # required by Postgres
.order('comment_count DESC') # Use COUNT(comments.*) in Oracle

Another way to solve this is by using counter_cache.



Related Topics



Leave a reply



Submit