开发者

Rails partial show latest 5 kases

I have my application setup with a few different partials working well. I have asked here how to get a partial working to show the latest entry in the kase model, but now I need to show the latest 5 entries in the kase model in a partial.

I have duplicated the show most recent one partial and it's working where I need it to but only shows the last entry, what do I need to change to show the last 5?

_recent_kases.html.erb

<% if Kase.most_recentfive %>
<h4>The most recent case reference is <strong><%= Kase.most_recentfive.jobno %></strong></h4>
<% end %>

kase.rb

  def self.most_recentfive
    first(:order => 'id DESC')
  end

Thanks,

Danny

EDIT

  def sel开发者_如何学编程f.most_recentfive
      all(:order => 'id DESC', :limit=>5)
    end

If I add the above code I get the following Error Message:

NoMethodError in Dashboard#index

Showing app/views/kases/_recent_kases.html.erb where line #2 raised:

undefined method `jobno' for #<Array:0x105984c60>
Extracted source (around line #2):

1: <% if Kase.most_recentfive %>
2: <h4>The most recent case reference is <strong><%= Kase.most_recentfive.jobno %></strong></h4>
3: <% end %>


  def self.most_recentfive
    all(:order => 'id DESC', :limit=>5)
  end

EDITED TO ADD:

Then, in your partial, to display the results, you do

<% if Kase.most_recentfive %>
<h4>The most recent five case references are
   <% Kase.most_recentfive.each do |k|%>
       <strong><%= link_to k.jobno, k %></strong><br />
   <% end %>
</h4>
<% end %>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜