How to select record by position

Asked

Viewed 176 times

1

can you tell me how I can select a record by position?

How would you search for Maria’s record? Obs: do not search by id or name

I found the take in the guide, but it looks for the first and the position. ex:

Person.take(3) // ele pegaria fabio e maria

Bench:

Fabio

Roberto

Maria

Joao

diego

2 answers

2


take actually takes the first 3 records (uses LIMIT for that) Ex:

Person.take(3)

would generate an sql: SELECT "usuarios".* FROM "usuarios" LIMIT 3

For few records something like the solution below, would solve:

Person.take(3)[2] # terceiro registro

But for a more performative solution I would not recommend this solution, since to make a query to a very distant position, this method would bring with it a huge amount of data.

For example, picking up item 1,000,000 would have to fetch these millions of records.

If you have other factors to limit this data, this solution can be used.

1

With the proposed record sample,

Person.third

would return the record 'maria'.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.