How to set default_scope for the whole application?

I know I can set default_scope for individual active record model, but
can I set one up for the whole application?

thanks

Well, I imagine you could, as long as all of your models had a key in
common. I mean, there’s no practical difference between vanilla Rails
and this:

default_scope order(‘id ASC’)

But if all of your models had a :position maybe you could do this

default_scope order(‘position ASC’)

and see what happens. I have my doubts – everything Controller-wise
inherits from ApplicationController, and what about controllers that
don’t just manage one model? This sort of thing has got to end badly
is my guess.

Walter

I want is to set
default_scope :order => ‘created_at ASC’
for all models on default. How is that done?

in what file do I put this or similar code?

On Feb 2, 2:00 pm, Slava M. [email protected] wrote:

I want is to set
default_scope :order => ‘created_at ASC’
for all models on default. How is that done?

I might be wrong but unless you don’t have IDs in your tables what you
are trying to do is equivalent to leaving things as they are since the
IDs are created in ascending order and retrieve in the same way (maybe
I read somewhere that Postgre might be an exception), the same thing
that happens with the values of created_at. That is, unless you are
planning on changing the value of created_at manually after the record
has already been created or the computer clock is reset or… pick
your choice of “strange” stuff here.

I want is to set
default_scope :order => ‘created_at ASC’
for all models on default. How is that done?

If you want to do this no matter what I think there should be a way to
do it at initialization. If you are using Rails 2.3.x you might be
able to do it in ‘new_rails_defaults.rb’ in folder config/
initizializers. I have not used Rails 3 yet so I don’t know where I
would make the change in that version, there is a comment in
‘new_rails_defaults.rb’ saying that the contents of that script will
be default in Rails 3 and the script can be removed when Rails is
upgraded to 3 so maybe the script will not run, but you can always try
anyway.

On 2 February 2011 03:15, slava [email protected] wrote:

I know I can set default_scope for individual active record model, but
can I set one up for the whole application?

I guess you could derive a class from ActiveRecord::Base and put the
default scope in there, then derive your models from that. I have not
tried it though.

Colin

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs