views:

26

answers:

2

Hello, Is there any way i can limit the results of an associated model?

This what i was trying to do :

<ul>
        <% account.logins.slice(0,5).sort_by(&:login_date).reverse.each do |login| -%>
          <li><%=h login.login_date.strftime("%d.%m.%Y")%></li>
        <% end -%>
</ul>

I'm trying to get the last five logins of the account. I cant seem to do it with account.logins(:limit=>5)

Thanks !

+1  A: 

Try this:

account.logins.find(:all, :limit => 5, :order => 'login_date desc')
Faisal
thanks! this worked!
r2b2
Don't use find(:all). It will be deprecated with Rails 3.0.
Simone Carletti
+3  A: 

or even shorter:

account.logins.all(:limit => 5, :order => 'login_date DESC')

=)

Staelen
+1 for the non-deprecated version
Simone Carletti