MVZ
MVZ

Reputation: 2250

Ruby on Rails - Selecting a range from a query

I'm doing a query to get all the purchases from the db. For example

orders = PurchaseOrders.all

I in the same query, how can I select only the first hundred orders(1-100) or just the next 100(101-200) etc..?

Thank you

Upvotes: 1

Views: 126

Answers (2)

Alexander Kireyev
Alexander Kireyev

Reputation: 10825

You can use limit and offset:

PurchaseOrders.limit(200).offset(100)

which meant start from 200 and take 100 records. More info here. Or with take:

PurchaseOrders.offset(100).take(400)

take 400 records starting from 100.

Upvotes: 2

acacia
acacia

Reputation: 1387

For the first 100 records;

orders = PurchaseOrders.first(100)

and last 100 records;

orders = PurchaseOrders.last(100)

or by IDs,

orders = PurchaseOrders.find([100, 201])

Upvotes: 0

Related Questions