从单独的控制器按 Sum 排序第 2 部分

发布于 2024-09-02 17:48:27 字数 1371 浏览 5 评论 0原文

好的,所以我 这个工作只是在添加一些控制器和重新定位一些代码之前很好。我觉得我在这里错过了一些非常简单的东西,但花了几个小时试图弄清楚发生了什么。情况是这样的。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

土豪我们做朋友吧 2024-09-09 17:48:27

我认为应该是

@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")

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")
时光是把杀猪刀 2024-09-09 17:48:27

啊...事实证明我已将 @sites 控制器代码从 SitesController 移至 QuestionsController Show 操作。然后我不得不将 /views/questions/show.html.erb 页面中的部分内容从 更改

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

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

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 %> 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文