从单独的控制器按 Sum 排序第 2 部分
好的,所以我 这个工作只是在添加一些控制器和重新定位一些代码之前很好。我觉得我在这里错过了一些非常简单的东西,但花了几个小时试图弄清楚发生了什么。情况是这样的。
class Question < ActiveRecord::Base
has_many :sites
end
我
class Sites < ActiveRecord::Base
belongs_to :questions
end
正在尝试按照站点表中 'like'
列总和的顺序显示我的站点。来自我的上一个 StackOverflow 问题 当在 /views/sites/index.html.erb
文件中调用部分时,我可以正常工作。然后,我将部分移动到 /views/questions/show.html.erb 文件
中调用,它成功显示了站点,但无法像从站点视图调用时那样对它们进行排序。
我从 /views/questions/show.html.erb 文件中调用部分内容,如下所示:
<%= render :partial => @question.sites %>
这是 SitesController#index 代码
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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为应该是
I think it should be
啊...事实证明我已将 @sites 控制器代码从 SitesController 移至 QuestionsController Show 操作。然后我不得不将 /views/questions/show.html.erb 页面中的部分内容从 更改
为
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
to