# Perform custom calculation on model and sort by the calculation

**URL:** <https://www.ruby-forum.com/t/perform-custom-calculation-on-model-and-sort-by-the-calculation/136574>\
**Category:** Rails\
**Created:** [May 3, 2008, 8:44pm UTC](https://www.ruby-forum.com/t/perform-custom-calculation-on-model-and-sort-by-the-calculation/136574 "2008-05-03T20:44:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![robert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e95f7d/32.png) [@robert](https://www.ruby-forum.com/u/robert)\
**Post date:** [May 3, 2008, 8:44pm UTC](https://www.ruby-forum.com/t/perform-custom-calculation-on-model-and-sort-by-the-calculation/136574/1 "2008-05-03T20:44:26Z")

</div>

Hi,

If anyone can point me in the right direction, I’d very appreciative.

I need to peform a calculation on a model (a number of columns) and  
use a few columns of the model of the person who is logged in. Once  
that calculation is done, I need to order the model by the resulting  
calculation. Is there a way to this in the model class where I create  
a temporary column on the fly and order by it?

Thanks,  
Robert

---

<div class="post-metadata">

**Author:** ![robert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e95f7d/32.png) [@robert](https://www.ruby-forum.com/u/robert)\
**Post date:** [May 3, 2008, 8:55pm UTC](https://www.ruby-forum.com/t/perform-custom-calculation-on-model-and-sort-by-the-calculation/136574/2 "2008-05-03T20:55:34Z")

</div>

On 3 May 2008, at 19:24, Robert wrote:

> Hi,
> 
> If anyone can point me in the right direction, I’d very appreciative.
> 
> I need to peform a calculation on a model (a number of columns) and  
> use a few columns of the model of the person who is logged in. Once  
> that calculation is done, I need to order the model by the resulting  
> calculation. Is there a way to this in the model class where I create  
> a temporary column on the fly and order by it?

SomeModel.find :all, :select =\> “\*, a + b + c as  
some\_calculation”, :order =\> ‘some\_calculation’  
(assuming you had columns a b c and were interested in their sum.

Fred

---

<div class="post-metadata">

**Author:** ![robert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e95f7d/32.png) [@robert](https://www.ruby-forum.com/u/robert)\
**Post date:** [May 3, 2008, 10:10pm UTC](https://www.ruby-forum.com/t/perform-custom-calculation-on-model-and-sort-by-the-calculation/136574/3 "2008-05-03T22:10:11Z")

</div>

Thanks.

I actually got it to work by reordering the array after the fact. The  
calculations needed were too involved and required a lot of case  
switches to calculate so I wasn’t able to do it on initial query.

This works for me:  
@SomeModel.sort! {|b,a| a.RankNumber \<=\> b.RankNumber }

It’s definitely not as efficient as doing it during the initial query,  
but it works.

On May 3, 2:54 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![robert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e95f7d/32.png) [@robert](https://www.ruby-forum.com/u/robert)\
**Post date:** [May 3, 2008, 10:25pm UTC](https://www.ruby-forum.com/t/perform-custom-calculation-on-model-and-sort-by-the-calculation/136574/4 "2008-05-03T22:25:29Z")

</div>

On Sat, May 3, 2008 at 10:09 PM, Robert [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I actually got it to work by reordering the array after the fact. The  
> calculations needed were too involved and required a lot of case  
> switches to calculate so I wasn’t able to do it on initial query.
> 
> This works for me:  
> @SomeModel.sort! {|b,a| a.RankNumber \<=\> b.RankNumber }
> 
> It’s definitely not as efficient as doing it during the initial query,  
> but it works.

There’s still a third option which is to precompute the rank (for  
example in before filters) and have it stored in the database. That  
way you can order by rank with straight SQL.

For example, the total of an invoice is computable from its parts, but  
having it cached in the database makes sorting invoice tables by total  
lighter.

Of course in your case this may not make sense, or it may not be worth  
the trouble depending on the details.
