2010-11-30 21 views
0

我想知道是否有一種方法來完成這個黑客,我正嘗試使用我的Ruby on Rails應用程序。我遇到了一些關聯問題,找不到解決方案。你可以see that question here尋找一個短期修復限制記錄顯示數量沒有:限制=> 10

在平均時間,我想記錄的顯示限制到一些像10但我不希望通過:limit => 10的方式做到這一點的控制器,因爲我經歷了所有的記錄,需要循環。我甚至不確定這是否可能,但我想我會問。

我的看法代碼:

<% @comments.each do |comment| %> 
    <% if comment.workout.user_id == current_user.id %><br/> 
    <%= link_to (comment.user.username), comment.user %> <br/> 
    <%= time_ago_in_words(comment.created_at) %> <br/> 
    <%= link_to (comment.workout.title), comment.workout %><br/> 
    <%= sanitize(simple_format(comment.body), :tags => %w(p)) %> 
    <% end %> 
<% end %> 

我控制器簡單地調用

@comments = Comment.all(:order => "created_at DESC") 
+0

當你的意思是你需要遍歷所有記錄,你的意思是在控制器? – 2010-11-30 18:49:53

回答

0

我會做所有的控制器。觀點不應該這樣做。你說你不想只限於10個,因爲你想要所有人並做一些過濾。你可以:

@comments = Comment.all(:joins => :workout, :conditions => {:workouts =>{:user_id => current_user.id}}, :order => "created_at DESC", :limit => 10) 
+0

這看起來可能會回答我的問題。雖然沒有找到名爲「訓練」的協會,但我得到這個錯誤;也許你拼錯了嗎?` – bgadoci 2010-11-30 18:53:37

0
@comments[0..9] 

@comments[10..-1] 
0

是在will-paginate寶石你要找的機器人?

添加寶石你的environment.rb並安裝它之後,你會修改你的控制器,使其調用PAGINATE而非findall

@comments = Comment.paginate(:all, :order => "created_at DESC", :per_page => 10) 

,然後在視圖中添加分頁控件(鏈接推進一個頁面,回去頁面,&角)

<%= will_paginate @comments %>