# Processing ActiveRecord

**URL:** <https://www.ruby-forum.com/t/processing-activerecord/127458>\
**Category:** Rails\
**Created:** [January 29, 2008, 1:41pm UTC](https://www.ruby-forum.com/t/processing-activerecord/127458 "2008-01-29T13:41:45Z")\
**Posts on this page:** 7\
**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 29, 2008, 1:41pm UTC](https://www.ruby-forum.com/t/processing-activerecord/127458/1 "2008-01-29T13:41:45Z")

</div>

Hi-

What I would like to do in the controller is select 2 rows from my DB  
and manipulate those rows to be rendered in the view.

result = MyModel.find(:all, :limit =\> 2, :select =\> column) # Gives me  
2 rows.  
@view\_stuff = result[0] - result[1] #Get error for not having method  
‘-’

Do I need to write a class method to manipulate the result set? Even  
if the column type is of type float?

Is there a better approach here?

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 29, 2008, 3:23pm UTC](https://www.ruby-forum.com/t/processing-activerecord/127458/2 "2008-01-29T15:23:53Z")

</div>

should be  
result[0].column - result[1].column  
i think

---

<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 29, 2008, 3:33pm UTC](https://www.ruby-forum.com/t/processing-activerecord/127458/3 "2008-01-29T15:33:59Z")

</div>

That’s what I thought it would be too, then I get the following:

“undefined method `column’ for -2268.0:Float”

Any ideas?

On Jan 29, 2:23 pm, Thorsten M. \<rails-mailing-l…@andreas-

---

<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 29, 2008, 3:49pm UTC](https://www.ruby-forum.com/t/processing-activerecord/127458/4 "2008-01-29T15:49:47Z")

</div>

pete wrote:

> That’s what I thought it would be too, then I get the following:
> 
> “undefined method `column’ for -2268.0:Float”
> 
> Any ideas?
> 
> On Jan 29, 2:23 pm, Thorsten M. \<rails-mailing-l…@andreas-

just checked it like that:  
foo=ShipmentDivisions.find(:all, :limit =\> 2, :select =\> :amount)

where ShipmentDivisions has a col “amount”  
foo[0].amount - foo[1].amount works fine

your  
result = MyModel.find(:all, :limit =\> 2, :select =\> column)

uses (at least as typed here) a var column which must be a string with a  
col name  
this you can’t use like  
result[0].column

you would have to use something like  
result[0][column]

if you need variable col names here. otherwise just use  
result[0].the\_absolute\_colname

---

<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 29, 2008, 3:57pm UTC](https://www.ruby-forum.com/t/processing-activerecord/127458/5 "2008-01-29T15:57:46Z")

</div>

AMAZING! That worked!!

Why do you have to do “result[0][column]” when column is a variable?

THANK YOU!!

On Jan 29, 2:49 pm, Thorsten M. \<rails-mailing-l…@andreas-

---

<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 29, 2008, 4:20pm UTC](https://www.ruby-forum.com/t/processing-activerecord/127458/6 "2008-01-29T16:20:46Z")

</div>

Perfect, thanks again for your help with this!

On Jan 29, 3:06 pm, Thorsten M. \<rails-mailing-l…@andreas-

---

<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 29, 2008, 4:06pm UTC](https://www.ruby-forum.com/t/processing-activerecord/127458/7 "2008-01-29T16:06:39Z")

</div>

> Why do you have to do “result[0][column]” when column is a variable?

easy enough:  
result[0] is an object and result[0].xxx calls a function of this object  
all columns are defined as functions for ActiveRecord objects  
but your column is a string, containing the column name. there is no  
function column, since there is no column with that name  
ActiveRecord::[] is just an alternative way to request the content of a  
col in a slightly more flexible way, by handing over the name as a  
string
