# ActiveRecord diff?

**URL:** <https://www.ruby-forum.com/t/activerecord-diff/127243>\
**Category:** Rails\
**Created:** [January 26, 2008, 9:58pm UTC](https://www.ruby-forum.com/t/activerecord-diff/127243 "2008-01-26T21:58:19Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![pete](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3ab097/32.png) [@pete](https://www.ruby-forum.com/u/pete)\
**Post date:** [January 26, 2008, 9:58pm UTC](https://www.ruby-forum.com/t/activerecord-diff/127243/1 "2008-01-26T21:58:19Z")

</div>

Hi-

I have a situation where I would like to get the difference of the  
last 2 records in each column in my DB.

So, lets say I have col1 and col2, with values below.

col1 = 1, 2  
col2 = 3, 4

What I would like to do is iterate through each column name (col1,  
col2) and get the difference of the last 2 records in each. Since it  
doesn’t look like there is a .diff command for active record, I’d like  
to just store them in a variable and take the difference manually.

I have tried this, but it doesn’t work, what am I doing wrong here?

# Give me the last 2 rows from col1?

result = Object.find(:all,:limit =\> 2, :select col1, :order =\>  
‘enterdate DESC’)  
diffof\_rows = result[0] - result[1]

Thanks!

---

<div class="post-metadata">

**Author:** ![pete](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3ab097/32.png) [@pete](https://www.ruby-forum.com/u/pete)\
**Post date:** [January 27, 2008, 5:17pm UTC](https://www.ruby-forum.com/t/activerecord-diff/127243/2 "2008-01-27T17:17:40Z")

</div>

And I guess the real question is, with the code below, should this  
retireve the last 2 records in the table (I think I should use ASC)?
