按投票排序微博

发布于 2024-11-04 02:30:36 字数 1879 浏览 0 评论 0原文

我不知道如何格式化这里的代码。我在这里重新列出了这个问题(以更好的方式): http://railsforum.com/viewtopic.php? id=43839

拥有一个具有投票功能的简单微帖子网站。我希望能够按 votes.size 显示发布的微帖子。我似乎无法让它发挥作用,希望得到一些指导。

模型:

class Micropost
  attr_accessible :content
  has_many :votes 
  belongs_to :user 
  default_scope :order => 'microposts.created_at DESC'


class Vote
  belongs_to :micropost

视图:

# microposts page
<% unless @picture.microposts.empty? %>
  <table class="microposts" summary="User microposts">
    <%= render @microposts %>
  </table>
<% end %>

# microposts partial
<tr>
  <td class="micropost">   
    <span class="gravatar">     
      <%= link_to gravatar_for(micropost.user, :size => '60'), micropost.user %>
    </span>
    <span class="content" >
      <%= micropost.content %>
    </span>
    <span id="vote_total_<%= micropost.id %>" class="micropost_votes">
      votes:<%= micropost.votes.size %>
    </span>
  <% if current_page?(root_path) && signed_in? %>
    <span id="vote_button">
      <%= button_to 'vote', micropost_votes_path(:micropost_id => micropost),
          :remote => true 
      %>
    </span>
  <% end %>  
  </td>
</tr>

控制器:

# VotesController
def create
  @micropost = Micropost.find(params[:micropost_id])
  @vote      = @micropost.votes.build params[:micropost_id]

  respond_to do |format|
    if @vote.save
      format.html { redirect_to @micropost }
      format.js        
    else
      format.html { redirect_to root }
    end
  end
end

我已经能够按created_at日期对微帖子进行排序,但我似乎无法按micropost.votes.size对微帖子进行排序。这似乎与根据不同模型的状态订购一个模型有关。

I can't figure out how to format the code here. I relisted the question here (in nicer fashion): http://railsforum.com/viewtopic.php?id=43839

Have a simple microposts site with voting functionality. I want to be able to display the posted microposts by votes.size. I can not seem to get it to work and would appreciate some guidance.

MODELS:

class Micropost
  attr_accessible :content
  has_many :votes 
  belongs_to :user 
  default_scope :order => 'microposts.created_at DESC'


class Vote
  belongs_to :micropost

VIEWS:

# microposts page
<% unless @picture.microposts.empty? %>
  <table class="microposts" summary="User microposts">
    <%= render @microposts %>
  </table>
<% end %>

# microposts partial
<tr>
  <td class="micropost">   
    <span class="gravatar">     
      <%= link_to gravatar_for(micropost.user, :size => '60'), micropost.user %>
    </span>
    <span class="content" >
      <%= micropost.content %>
    </span>
    <span id="vote_total_<%= micropost.id %>" class="micropost_votes">
      votes:<%= micropost.votes.size %>
    </span>
  <% if current_page?(root_path) && signed_in? %>
    <span id="vote_button">
      <%= button_to 'vote', micropost_votes_path(:micropost_id => micropost),
          :remote => true 
      %>
    </span>
  <% end %>  
  </td>
</tr>

CONTROLLERS:

# VotesController
def create
  @micropost = Micropost.find(params[:micropost_id])
  @vote      = @micropost.votes.build params[:micropost_id]

  respond_to do |format|
    if @vote.save
      format.html { redirect_to @micropost }
      format.js        
    else
      format.html { redirect_to root }
    end
  end
end

I've been able to sort the microposts by the created_at date, but I can not seem to sort the microposts by the micropost.votes.size. It seems to have something to do with ordering one model based on the state of a different model.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

a√萤火虫的光℡ 2024-11-11 02:30:36
Micropost.all(:select => "#{Micropost.table_name}.*, COUNT(#{Vote.table_name}.id) number_of_votes",
         :joins => :votes,
         :order => "number_of_votes desc")
Micropost.all(:select => "#{Micropost.table_name}.*, COUNT(#{Vote.table_name}.id) number_of_votes",
         :joins => :votes,
         :order => "number_of_votes desc")
幽梦紫曦~ 2024-11-11 02:30:36

嗯,这是我找到的最好的解决方案。在渲染微帖子的视图上,我添加了:

.sort_by { |micropost| micropost.votes.size }

所以实际的渲染代码如下所示:

<%= render @microposts.sort_by { |micropost| micropost.votes.size } %>

我实际上颠倒了我的帖子,以便投票总数最高的帖子位于顶部。这很简单:

<%= render @microposts.sort_by { |micropost| micropost.votes.size }.reverse %>

我不知道这是否会造成数据库性能问题,但它似乎运行良好。

Well, this was the best solution that I found. On the view where the microposts are rendered, I added:

.sort_by { |micropost| micropost.votes.size }

So the actual render code looks like this:

<%= render @microposts.sort_by { |micropost| micropost.votes.size } %>

I actually reversed my posts so that the posts with the highest vote totals were on top. This is easy:

<%= render @microposts.sort_by { |micropost| micropost.votes.size }.reverse %>

I have NO idea if this will create problems with database performance, but it seems to be working well.

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