# Default order for models?

**URL:** <https://www.ruby-forum.com/t/default-order-for-models/67156>\
**Category:** Rails\
**Created:** [September 1, 2006, 3:45am UTC](https://www.ruby-forum.com/t/default-order-for-models/67156 "2006-09-01T03:45:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![joe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b9e5f3/32.png) [@joe](https://www.ruby-forum.com/u/joe)\
**Post date:** [September 1, 2006, 3:45am UTC](https://www.ruby-forum.com/t/default-order-for-models/67156/1 "2006-09-01T03:45:03Z")

</div>

Is there a way to specify the default order for models? So you could  
just do this:

items.find(:all).each do …

rather than:

items.find(:all, :order=\>‘name’).each do …

Joe

---

<div class="post-metadata">

**Author:** ![joe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b9e5f3/32.png) [@joe](https://www.ruby-forum.com/u/joe)\
**Post date:** [September 1, 2006, 8:35pm UTC](https://www.ruby-forum.com/t/default-order-for-models/67156/2 "2006-09-01T20:35:18Z")

</div>

Hi Joe –

On 31-Aug-06, at 9:45 PM, Joe R. wrote:

> Is there a way to specify the default order for models?

No, there isn’t.

For the sake of consistency, I think there should be – when you’re  
doing associations you can specify the order there, (as in  
has\_many :foos, :order =\> ‘created\_at desc’), so why not a default  
order for the model?

You could always override the find method in your model class,  
though. Something like:

def self.find(\*args)  
if args.first.is\_a?(Symbol)  
args \<\< {} if args.size \< 2  
args.last.reverse\_merge! :order =\> “#{self.table\_name}.name”  
super args.first, args.last  
else  
super  
end  
end

That would give you a default order by on ‘name’, which could be  
overridden by specifying the :order option explicitly, as per usual.

HTH

/Jeff

---

<div class="post-metadata">

**Author:** ![joe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b9e5f3/32.png) [@joe](https://www.ruby-forum.com/u/joe)\
**Post date:** [September 1, 2006, 9:53pm UTC](https://www.ruby-forum.com/t/default-order-for-models/67156/3 "2006-09-01T21:53:29Z")

</div>

take a look at this article, it may be of some help for you.

[http://habtm.com/articles/2006/02/22/nested-with\_scope](http://habtm.com/articles/2006/02/22/nested-with_scope)

this article basically explains various ways to utilize with\_scope.  
one of the methods addresses a very similar situation to yours,  
however, it is done at the controller level rather than the model. it  
still may be useful to you.

Chris
