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


#1

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


#2

You could always define your own subroutine :slight_smile:


#3

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. :slight_smile:


#4

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?

Override the find method in that model?

or

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,


#5

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