Reputation: 7200
This question is kind of an extension of Another question, so I'll be using that as a baseline for this question.
Say I have a bunch of tickets that I would like fetched and sorted by name. That's easy enough, just do:
@tickets = Ticket.find(:all, :order => 'name')
However, is there a way to tell Ticket.find that I only want the first x tickets? I'm betting there's a way to do this with straight SQL, and I'm sure I could just take @tickets
and shorten it to the first x elements, but I'd rather do it a straight Ruby way that doesn't acquire all the tickets (I have an awful lot of "tickets" and I don't really want to get them all, that could cost a lot of CPU time).
All help is appreciated and thanks in advance!
Upvotes: 2
Views: 500
Reputation: 21
This is just an extension of the 'limit' parameter in SQL. For example, SELECT * FROM ticket ORDER BY name LIMIT 4
. It is, thus, available in any ORM, including ActiveRecord.
Upvotes: 1
Reputation: 13433
..and if you're using Rails 3, you could make it
@tickets = Ticket.order('name').limit(x).all
Upvotes: 1
Reputation: 9857
All you want to do is:
@tickets = Ticket.find(:all, :order => 'name', :limit => x)
Where x
is how many records you want.
See the Rails documentation for more conditions you can put in your find
. (Search for VALID_FIND_OPTIONS
.)
Upvotes: 5