r2b2
r2b2

Reputation: 1275

Using :limit and :order in the associated model

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 !

Upvotes: 0

Views: 975

Answers (2)

Staelen
Staelen

Reputation: 7841

or even shorter:

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

=)

Upvotes: 2

Faisal
Faisal

Reputation: 18988

Try this:

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

Upvotes: 1

Related Questions