Miotsu
Miotsu

Reputation: 1776

First/last elements of ActiveRecord_Relation as relation

I perform a query like, for example:

CarrotTopProps.where(funny: false)

which returns an ActiveRecord_Relation object.

Now, I need the first, say, 10 results of that query, I can write:

CarrotTopProps.where(funny: false).first(10)

but that will give me an Array object. Crazy as it might sound, I need the resulting object to maintain the ActiveRecord_Relation class.

Is there a way to do that?

Upvotes: 1

Views: 467

Answers (1)

Martin Schneider
Martin Schneider

Reputation: 3268

Use limit and - if you want the "first" objects, combine it with order.

CarrotTopProps.where(funny: false).limit(10)

yields an ActiveRecord::Relation

http://api.rubyonrails.org/classes/ActiveRecord/QueryMethods.html#method-i-limit

Upvotes: 4

Related Questions