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?

On Sep 19, 7:31 am, bingo bob [email protected]

Bingo Bob!

Here’s the code I used to test this:
http://pastie.org/622639

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?

Gavin

http://handyrailstips.com

On Sep 19, 7:31 am, bingo bob [email protected]

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?

Gavin

On Sep 19, 2:40 pm, bingo bob [email protected]

in case it helps

$ ./script/about

About your application’s environment
Ruby version 1.8.7 (universal-darwin10.0)
RubyGems version 1.3.5
Rack version 1.0 bundled
Rails version 2.3.2
Active Record version 2.3.2
Action Pack version 2.3.2
Active Resource version 2.3.2
Action Mailer version 2.3.2
Active Support version 2.3.2
Edge Rails revision f0d962381a69213d3089a30ca1300826da9ec7e7
Application root /Users/rupe/Sites/foo
Environment development
Database adapter sqlite3
Database schema version 20090912105013

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?

Gavin

On Sep 19, 3:07 pm, bingo bob [email protected]
wrote:

Posted viahttp://www.ruby-forum.com/.
On Sep 19, 3:07 pm, bingo bob [email protected]

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 !
:-).

bb

you’re welcome :slight_smile:

On Sep 19, 4:04 pm, bingo bob [email protected]

Gavin M. wrote:

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?

Gavin

On Sep 19, 2:40�pm, bingo bob [email protected]

Yeah sure - I don’t want to break anything though, and i’ve “frozen”
rails into vendor.

Is it easy to upgrade and get it into my git repo and deploy, I guess it
is!?

What steps?

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