MikeHolford
MikeHolford

Reputation: 1931

Rails "order_by" an associated table

I have two database tables, one called "Recipes" and another "Likes".

When I list my recipes on my website I can easily order them by one of the columns within the recipe table. For example by the date they are created at:

<% @recipes.order("created_at desc").limit(16).each do |i| %>
  <% if i.live? %>
    <%=link_to image_tag(i.image(:fixed), :class => "newest-recipes"), i %>
  <% end %>
<% end %>

What I really want to do is to order them by the number of likes they have. To display the number of likes on the recipes page I use this piece code:

<%= @recipe.likers.count %>

Is there a way of ordering the recipes by @recipe.likers.count?

Please let me know if you require additional information such as the recipe controller or model.

Thank you!

Upvotes: 1

Views: 107

Answers (2)

gabrielhilal
gabrielhilal

Reputation: 10769

I would go for the @Ju Liu answer (+1). However, in case you cannot add the new column to the existing table, you can create a scope in your Recipe model:

class Recipe < ActiveRecord::Base
  has_many :likes
  scope :likes_count,
    select("recipes.id, ANY_OTHER_COLUMN_YOU_NEED_HERE, count(likes.id) AS likes_count").
    joins(:likes).
    group("recipes.id").
    order("likes_count DESC")
end

@recipes = Recipe.likes_count

Upvotes: 0

Ju Liu
Ju Liu

Reputation: 3999

You should take a look at http://guides.rubyonrails.org/association_basics.html#counter-cache

Basically, add a new integer column to the recipes table called likes_count, then edit the Like model to look like:

class Like < ActiveRecord::Base
  belongs_to :recipe, counter_cache: true
end

Then Rails will automatically update it when necessary and you can just order by that column:

@recipes.order(:likes_count)

Upvotes: 1

Related Questions