Ordering by Sum from a separate controller part 2

Posted by bgadoci on Stack Overflow See other posts from Stack Overflow or by bgadoci
Published on 2010-05-20T03:44:48Z Indexed on 2010/05/20 3:50 UTC
Read the original article Hit count: 215

Ok, so I had this working just fine before making a few controller additions and the relocation of some code. I feel like I am missing something really simple here but have spent hours trying to figure out what is going on. Here is the situation.

class Question < ActiveRecord::Base
  has_many :sites
end

and

class Sites < ActiveRecord::Base
  belongs_to :questions
end

I am trying to display my Sites in order of the sum of the 'like' column in the Sites Table. From my previous StackOverflow question I had this working when the partial was being called in the /views/sites/index.html.erb file. I then moved the partial to being called in the /views/questions/show.html.erb file and it successfully displays the Sites but fails to order them as it did when being called from the Sites view.

I am calling the partial from the /views/questions/show.html.erb file as follows:

<%= render :partial => @question.sites %>

and here is the SitesController#index code

class SitesController < ApplicationController

  def index
    @sites = @question.sites.all(:select => "sites.*, SUM(likes.like) as like_total",
             :joins => "LEFT JOIN likes AS likes ON likes.site_id = sites.id",
             :group => "sites.id",
             :order => "like_total DESC")

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @sites }
    end
  end

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about ruby