views:

33

answers:

2

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
A: 

I think it should be

@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 => "SUM(likes.like) DESC")
Salil
When I put that in the sites controller index action it doesn't do anything. Does it need to go somewhere in the questions controller?
bgadoci
A: 

Ah...turns out that I had move the @sites controller code from the SitesController to the QuestionsController Show action. I then had to change my partial in the /views/questions/show.html.erb page from

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

to

<%= render :partial => @sites %> 
bgadoci