限制Rails App中帖子索引的评论

时间:2011-04-12 02:56:05

标签: ruby-on-rails comments associations posts

我是rails的新手,并试图将帖子索引页面上显示的评论数限制为2.

以下是我的posts_controller:

class PostsController < ApplicationController

  before_filter :authorize, :except => [:index, :show]

  # GET /posts
  # GET /posts.xml
  def index
    @posts = Post.all(:include => :comments, :order => "created_at DESC")
    @comments = Comment.find(:all, :order => "created_at DESC", :limit => 1)

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

  # GET /posts/1
  # GET /posts/1.xml
  def show
    @post = Post.find(params[:id])

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

  # GET /posts/new
  # GET /posts/new.xml
  def new
    @post = Post.new

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

  # GET /posts/1/edit
  def edit
    @post = Post.find(params[:id])
  end

  # POST /posts
  # POST /posts.xml
  def create
    @post = Post.new(params[:post])

    respond_to do |format|
      if @post.save
        format.html { redirect_to(@post, :notice => 'Post was successfully created.') }
        format.xml  { render :xml => @post, :status => :created, :location => @post }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @post.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /posts/1
  # PUT /posts/1.xml
  def update
    @post = Post.find(params[:id])

    respond_to do |format|
      if @post.update_attributes(params[:post])
        format.html { redirect_to(@post, :notice => 'Post was successfully updated.') }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @post.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /posts/1
  # DELETE /posts/1.xml
  def destroy
    @post = Post.find(params[:id])
    @post.destroy

    respond_to do |format|
      format.html { redirect_to(posts_url) }
      format.xml  { head :ok }
    end
  end
end

以下是我的帖子模型

  

class Post&lt;的ActiveRecord ::基

     

has_attached_file:照片,:样式   =&GT; {:medium =&gt; “600x600&gt;”,:thumb =&gt; “100×100&gt;” 中},       :storage =&gt; :S3,       :s3_credentials =&gt; “#{RAILS_ROOT} /config/s3.yml”       :path =&gt; “/:附接/:ID /:风格/:文件名”

     

has_many:评论

     

验证:name,:presence =&gt;真正   验证:title,:presence =&gt;真正,                     :length =&gt; {:minimum =&gt; 5}

     

以下是我的帖子索引视图

<table>
  <tr>
     <th>BoxScore</th>
   <th>Content</th>
  </tr>
</table>

<% @posts.each do |post| %>
    <%= image_tag post.photo.url(:medium), :class =>"floatleft" %>
    <p>
  <%= post.content %>
    </p>
    <div class="comments center">
    <h3>Comments:</h3>
        <%= render :partial => post.comments.reverse %>
   <div class="links">
    <% if admin? %>
        <%= link_to "New Post", new_post_path %>
        <%= link_to 'Edit', edit_post_path(post) %>
    <%= link_to 'Destroy', post, :confirm => 'Are you sure?', :method => :delete %>
    <% end %>
    <%= link_to 'Comment on the Game', post %>
    </div>
</div>
<% end %>

</div>

评论部分

<% div_for comment do %>
    <p>
        <strong>Posted <%= time_ago_in_words(comment.created_at) %> ago
        </strong>
                by
                <br/>
                <%= h(comment.commenter) %>
                <br/>
        <%= h(comment.body) %>
                <br/>
                <%= link_to 'More Comments', comment.post %>
    </p>
<% end %>

我没有收到错误消息,我只是不知道如何限制我们在帖子索引页面上呈现的评论数量。感谢

1 个答案:

答案 0 :(得分:1)

不幸的是,您无法在预先加载的关联上指定条件。同样,你不能根据条件限制返回的行数(据我所知,虽然有许多我不了解的SQL函数)。

所以你坚持使用:

  1. 加载您在查询中显示的帖子的所有评论,并限制应用中显示的数字。
  2. 仅为您正在显示 的每个帖子加载2条评论。
  3. 最佳解决方案取决于您的使用案例。如果您希望仅显示5个帖子并且每个帖子都有数千个评论,则选项1可能不是非常高效,选项2可能是一个很好的解决方案。如果您希望每页显示更多帖子并且对任何一个帖子只有少量评论(更可能的情况),那么第一个选项将是您最好的选择。

    选项1

    # controller
    @posts = Post.limit(20).all
    @comments = Comment.find(@posts.collect &:id).group_by &:post_id
    
    # view
    <% @comments[@post.id].first(2).each do |comment| %>
      ...
    <% end %>
    

    选项2

    # controller
    @posts = Post.limit(5).all
    
    # view
    <% post.comments.limit(2).each do |comment| %>