2011-04-12 49 views
0

我新的轨道,并试图对我的文章索引页面显示评论数限制为2限制在帖子指数在评论Rails应用程序

下面是我的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:光度计0::样式 => {:medium =>“600x600>”,:thumb =>“100x100>”}, :storage =>:s3, :s3_credentials =>“#{RAILS_ROOT}/config/s3。 YML”, :路径=> “/:附接/:ID /:风格/:文件名”

的has_many:评论

验证:姓名,:存在=>真 验证:标题,:存在= >真, :长度=> {:最小=> 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 %> 

,我没有得到一个错误信息,我只是不知道如何限制的意见,我们正在绘制的金额帖子索引页面。谢谢

+0

你应该张贴您的部分太多,多数民众赞成与评论的内容,说什么样的错误,你这个获得。 – 2011-04-12 03:07:17

回答

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| %> 
+0

如果你不想确保你的结果是一个数组,那么调用'all'通常是一个坏主意。 'ActiveRecord :: Relation'响应'Array'所具有的大多数方法,所以你可以用'each'或'map' /'collect'对进行迭代。但是'所有'都渴望加载结果集,从性能的角度来看,这不是一个好主意。 – 2013-08-29 14:31:32

+0

@GaborGarami这*可以是真实的,但并不总是如此。如果您的应用程序完全适合Rails的CRUD,那么您可能会放弃让您的控制器组装Arel查询,您的视图层将通过迭代来执行。但是,在很多情况下,如果您需要在检索后处理数据,组装自定义缓存键或修改这些对象(例如'@ post.touch(:last_viewed)'),这种情况会崩溃。让视图直接触发SQL查询(模型的责任)也是不太理想的,无需控制器的帮助。 – coreyward 2013-08-29 18:16:01

+0

是的,例外总是在这里:-)。但至少在答案的情况下,“全部”是不必要的。 – 2013-08-30 23:25:00

相关问题