# Active record position

**URL:** <https://www.ruby-forum.com/t/active-record-position/127244>\
**Category:** Rails\
**Created:** [January 26, 2008, 10:42pm UTC](https://www.ruby-forum.com/t/active-record-position/127244 "2008-01-26T22:42:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![flnative](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/5daacb/32.png) [@flnative](https://www.ruby-forum.com/u/flnative)\
**Post date:** [January 26, 2008, 10:42pm UTC](https://www.ruby-forum.com/t/active-record-position/127244/1 "2008-01-26T22:42:20Z")

</div>

Hi @ all  
How can I find out the number of the position of a record in the  
database?

i.e:

id firstname

* * *

2 bryan -\> that’s position 1  
5 silvester -\> that’s position 2  
19 michael -\> that’s position 3

thanks for helping!

---

<div class="post-metadata">

**Author:** ![flnative](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/5daacb/32.png) [@flnative](https://www.ruby-forum.com/u/flnative)\
**Post date:** [January 26, 2008, 11:18pm UTC](https://www.ruby-forum.com/t/active-record-position/127244/2 "2008-01-26T23:18:37Z")

</div>

2008/1/26, K. R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> 5 silvester → that’s position 2  
> 19 michael → that’s position 3

Well, the existence of positions requires the definition  
of an order. I assume that you want the records ordered  
by id, from lowest to highest.

Instead of asking “what is the position of record x”, we  
can ask “how many records are smaller than x”. Well,  
that’s a simple SQL query:

```
select count(*) from records where id < 42;

```

assuming the record whose position we want to  
know has the id 42.

Or, in Rails speak:

```
position = Record.count(:conditions => ["id < ?",

```

id\_of\_object\_whose\_position\_we\_want\_to\_know])

HTH,  
Stefan

---

<div class="post-metadata">

**Author:** ![flnative](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/5daacb/32.png) [@flnative](https://www.ruby-forum.com/u/flnative)\
**Post date:** [January 26, 2008, 11:46pm UTC](https://www.ruby-forum.com/t/active-record-position/127244/3 "2008-01-26T23:46:15Z")

</div>

2008/1/26, K. R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > 19 michael → that’s position 3

Without reference to, as a starting point, an index or indexing function  
(e.g. “order by”), position is pretty much indeterminate. It depends on  
how the free space algorithm of the database works and much else. In  
fact deleting a record form another table might cause some space  
management function to be invoked and the position changed. (e.g.  
b-trees). Even this may be changed if you use a disk mapping technology  
like LVM.

When we write the database out as you have done above we are  
linearizing. The structure and ‘pointers’ of the actual database is not  
necessarily in any way linear. It may be optimized in any one of a  
number of ways.

The way you’ve written it out its “order by id”. That is in accord with  
the default index that Rails uses when it creates a table.

Perhaps the question you’re asking is “what is the offset of a record in  
the table when its ordered by id?”. That’s meaningful regardless of the  
database technology and how it allocates the position of record on the  
disk.

–  
life, n.:  
A whim of several billion cells to be you for a while.

---

<div class="post-metadata">

**Author:** ![flnative](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/5daacb/32.png) [@flnative](https://www.ruby-forum.com/u/flnative)\
**Post date:** [January 27, 2008, 12:36pm UTC](https://www.ruby-forum.com/t/active-record-position/127244/4 "2008-01-27T12:36:22Z")

</div>

On 26/01/2008, Stefan L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> select count(*) from records where id < 42;
> 
> ```

```
select count(*) from records where id <= 42;

```

-Thomas

–  
Thomas P.  
removed\_email\_address@domain.invalid  
removed\_email\_address@domain.invalid  
BÃ¼ro: 030 - 830 353 88  
mobil: 0176 - 75 03 03 04  
Privat: 030 - 49 78 37 06

> **[thopre](https://thopre.wordpress.com/)**
>
> Entwicklungen für die Bühne des Lebens

[http://www.thopre.com/](http://www.thopre.com/)
