# Order by dynamic field

**URL:** <https://www.ruby-forum.com/t/order-by-dynamic-field/88453>\
**Category:** Rails\
**Created:** [March 11, 2007, 11:52am UTC](https://www.ruby-forum.com/t/order-by-dynamic-field/88453 "2007-03-11T11:52:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Justas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9fc29f/32.png) [@Justas](https://www.ruby-forum.com/u/Justas)\
**Post date:** [March 11, 2007, 11:52am UTC](https://www.ruby-forum.com/t/order-by-dynamic-field/88453/1 "2007-03-11T11:52:41Z")

</div>

Hello,

If I have the following model:

class MyModel \< ActiveRecord::Base  
def self.some\_calculations(id)  
…  
end  
end

Is it possible to use MyModel.find() and order the results by my defined  
method self.some\_calculations(id)?

–  
Thanks in advance,  
Justas

---

<div class="post-metadata">

**Author:** ![Justas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9fc29f/32.png) [@Justas](https://www.ruby-forum.com/u/Justas)\
**Post date:** [March 11, 2007, 3:11pm UTC](https://www.ruby-forum.com/t/order-by-dynamic-field/88453/2 "2007-03-11T15:11:49Z")

</div>

Yes, why not?

class MyModel \< ActiveRecord::Base  
def self.some\_calculations(id)  
find(:all, :conditions =\> [‘id != ?’, id], :order =\> ‘rand()’)  
end  
end

> > MyModel.some\_calculations(66)

OR drop the self and have an instance method…

class MyModel \< ActiveRecord::Base  
def some\_calculations  
find(:all, :conditions =\> [‘id != ?’, id], :order =\> ‘rand()’)  
end  
end

> > MyModel.find(66).some\_calculations

---

<div class="post-metadata">

**Author:** ![Justas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9fc29f/32.png) [@Justas](https://www.ruby-forum.com/u/Justas)\
**Post date:** [March 11, 2007, 1:58pm UTC](https://www.ruby-forum.com/t/order-by-dynamic-field/88453/3 "2007-03-11T13:58:40Z")

</div>

On Mar 11, 2007, at 11:52 AM, Justas wrote:

> method self.some\_calculations(id)?  
> To accomplish that you need to fetch all the involved rows, and order  
> the objects in Ruby land:

@models = MyModel.find\_all\_by\_foo(foo).sort {|a, b| … }

Of course that may be too expensive, depending on the amount of data.  
If that was the case, you could consider maintaining those  
calculations via AR callbacks and cache them in some column. That  
way, you’d delegate ordering to the database.

– fxn

---

<div class="post-metadata">

**Author:** ![Justas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9fc29f/32.png) [@Justas](https://www.ruby-forum.com/u/Justas)\
**Post date:** [March 11, 2007, 4:58pm UTC](https://www.ruby-forum.com/t/order-by-dynamic-field/88453/4 "2007-03-11T16:58:00Z")

</div>

On Mar 11, 2007, at 3:10 PM, eden li wrote:

> class MyModel \< ActiveRecord::Base  
> def self.some\_calculations(id)  
> find(:all, :conditions =\> [‘id != ?’, id], :order =\> ‘rand()’)  
> end  
> end

Justas wants to order the rows according to the result of applying  
some\_calculations() to them.

– fxn
