0

我是 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

下面是我的帖子模型

类 Post < ActiveRecord::Base

has_attached_file :photo, :styles => { :medium => "600x600>", :thumb => "100x100>" }, :storage => :s3, :s3_credentials => "#{RAILS_ROOT}/config/s3.yml ", :path => "/:attachment/:id/:style/:filename"

has_many:评论

验证 :name, :presence => true 验证 :title, :presence => true, :length => { :minimum => 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 %>

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

4

1 回答 1

1

不幸的是,您不能在急切加载的关联上指定条件。同样,您不能根据条件限制返回的行数(据我所知,尽管有很多关于 SQL 函数的事情我不知道)。

所以你被困在其中之一:

  1. 加载您在查询中显示的帖子的所有评论,并限制应用程序中显示的数量。
  2. 为您单独显示的每个帖子仅加载 2 条评论。

最佳解决方案取决于您的用例。如果您希望只显示 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| %>
于 2011-04-12T03:16:48.087 回答