silent scope ?

I do this in my model.

  named_scope :popular, :order => 'name', :conditions => ['popular_resort = ?', true]   default_scope :order => 'resort_height DESC, name'

By default my resorts are sorted by resort_height then name - works fine.

Then when I do this Resort.popular I wish to override the sort order

I want my popular resorts sorted by name NOT altitude

It does not work? Resort.popular does this..

SELECT * FROM "resorts" WHERE (popular_resort = 't') ORDER BY resort_height DESC, name

i.e. it's still doing the ORDER BY resort_height DESC bit which I don't want.

What am I missing here?

Bingo Bob,

Just tried this out on my own machine.

Works fine for me.

What version of ActiveRecord are you using?

Bingo Bob!

Here's the code I used to test this:

Copy/paste that into a new .rb file and run it.

If the results come out as you'd expect ("resort_1" then "resort_5") then the issue is in your code somplace. If they don't, then it may be your version of ActiveRecord.

Hope that helps?


I get this running your program

rupes-macbook:Sites rupe$ ruby scope_test.rb resort_5 resort_5 rupes-macbook:Sites rupe$

hmmm, that doesn't seem right? Then, what's going on here.

Bob, I'm running Rails 2.3.4 now - so ActiveRecord 2.3.4

This definitely works fine on my machine so I'd guess it was a bug in ActiveRecord 2.3.2.

would you consider updating your application to rails 2.3.4?


Change the rails version constant in environment.rb to 2.3.4

then run:

rake rails:freeze:gems # freezes the newer version in vendor

rake rails:update # updates any necessary config and script files

I'm pretty sure that's all you need to do.

It shouldn't break anything although you might notice some behaviors changing (like the default_scope working as expected) so re-run all of your tests to be sure.

If you want to try it on a separate git branch to be safe:

git add . # add all the current files git commit -a -m "Updating to Rails 2.3.4" # commit current settings to git git branch update # create a new branch git checkout update # move to new branch

# then do the necessary steps to update # make sure everything is working as expected

git checkout master # move back to master branch git merge update # merge the changes if you're happy with them git merge update

That oughtta do it. Let me know if that solves the issue?


You nailed it. The scoping and sorting is working as expected. Can't believe it was a bug in rails! Thanks a mil. My app is now running on 2.3.4 to boot, which I guess is better by .2 ! :-).


you're welcome :slight_smile: