views:

79

answers:

1

Hi Everyone,

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 self.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 %>
+1  A: 
  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 %>
JacobM
I tried that a second ago but got the error message I added to my question above.
dannymcc
You're getting an error because your "most_recentfive" method is returning an array (of kase) and array doesn't have a jobno. You need to loop through the five kases in the array and output each of them.
JacobM
Could you elaborate - I'm pretty stupid when it comes to rails.I need to add five calls for kases in the partial?
dannymcc
Edited to add how to display the results -- at the right point in your ERB, you iterate over the kases and display each one.
JacobM
Great, that worked a treat thanks!If I wanted to make them link to the relevant kase show view would I just add link_to?
dannymcc
Yep! And remember, if this answer worked for you, please select it as your accepted answer by clicking the checkmark to the left of the answer. Thanks!
JacobM
Done that, marked the answer as brilliant!I added link_to but it just outputs a link to /.<% Kase.most_recentfive.each do |k|%> <ul class="dashboard_recentkases"> <li><%=link_to k.jobno%></li> </ul> <% end %>
dannymcc
You need to specify both the text to put in the link (k.jobno) and the item to link to (k). I've altered my partial example above to include a link_to.
JacobM
Great, thank you so much!
dannymcc