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
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
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
take a look at this article, it may be of some help for you.
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
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs