# Set default order for find(:all) in a model

**URL:** <https://www.ruby-forum.com/t/set-default-order-for-find-all-in-a-model/48807>\
**Category:** Rails\
**Created:** [November 25, 2005, 8:01am UTC](https://www.ruby-forum.com/t/set-default-order-for-find-all-in-a-model/48807 "2005-11-25T08:01:43Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![liquid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/47e85d/32.png) [@liquid](https://www.ruby-forum.com/u/liquid)\
**Post date:** [November 25, 2005, 8:01am UTC](https://www.ruby-forum.com/t/set-default-order-for-find-all-in-a-model/48807/1 "2005-11-25T08:01:43Z")

</div>

Hi everyone

Is there a way that I can get the find(:all) method to return in a  
specified  
order, without including the :order argument every time?

Guess I just don’t like typing…

Cheers

---

<div class="post-metadata">

**Author:** ![liquid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/47e85d/32.png) [@liquid](https://www.ruby-forum.com/u/liquid)\
**Post date:** [November 25, 2005, 10:25am UTC](https://www.ruby-forum.com/t/set-default-order-for-find-all-in-a-model/48807/2 "2005-11-25T10:25:43Z")

</div>

You could always define your own subroutine 🙂

---

<div class="post-metadata">

**Author:** ![liquid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/47e85d/32.png) [@liquid](https://www.ruby-forum.com/u/liquid)\
**Post date:** [November 25, 2005, 10:32am UTC](https://www.ruby-forum.com/t/set-default-order-for-find-all-in-a-model/48807/3 "2005-11-25T10:32:29Z")

</div>

liquid wrote:

> Is there a way that I can get the find(:all) method to return in a  
> specified order, without including the :order argument every time?

A default table order sounds like a MySQL thing. You can specify an  
order for your associations though:

```
has_many :velvet_elvis_posters, :order => 'velvetiness desc'

```

> Guess I just don’t like typing…

This is a good thing. If it weren’t for people like you, we’d still be  
using Java. 🙂

---

<div class="post-metadata">

**Author:** ![liquid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/47e85d/32.png) [@liquid](https://www.ruby-forum.com/u/liquid)\
**Post date:** [November 25, 2005, 10:47am UTC](https://www.ruby-forum.com/t/set-default-order-for-find-all-in-a-model/48807/4 "2005-11-25T10:47:06Z")

</div>

On Friday 25 November 2005 07:01, Liquid wrote:

> Hi everyone
> 
> Is there a way that I can get the find(:all) method to return in a  
> specified order, without including the :order argument every time?

1. 

Override the find method in that model?

or

1. 

class Foo \< ActiveRecord::Base  
…  
def my\_find  
find(:all, …)  
end  
…  
end

Then use my\_find where you would have used find.

My non-expert opinion is that 2. is slightly better than 1. because you  
may at some point need another version of find sorted differently and  
you may make life more difficult by modifying the default behaviour of  
find rather than adding a new version with its own behaviour.

> Guess I just don’t like typing…
> 
> Cheers

HTH,

---

<div class="post-metadata">

**Author:** ![liquid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/47e85d/32.png) [@liquid](https://www.ruby-forum.com/u/liquid)\
**Post date:** [November 25, 2005, 10:59am UTC](https://www.ruby-forum.com/t/set-default-order-for-find-all-in-a-model/48807/5 "2005-11-25T10:59:11Z")

</div>

Thanx for the suggestions guys…

I have thought of trying to extend ActiveRecord… I’m not completely  
sure  
how to do that yet. What I’ve been thinking of is to overwrite the find  
method, and if there is no :order argument defined, I would like to put  
an  
instance or class variable in my model, that I include as the :order  
argument

eg

class SomeModel \< ActionRecord  
@@order = ‘the\_order\_field, another\_field’  
…

end

Then whenever the find method is called. If there is no :order  
specified,  
order by @@order. If there is no @@order specified just execute the  
original find…

The reason for this is because I’m trying to use an engine, but I want  
to  
specify what order the lists are presented without going into that code  
at  
all.

Any help would be great.

Cheers
