nested named_scopes and :order

I have a question about peoples opinions about how nested named_scopes should handle :order options.

What are your opinions about the correct behavior for the following chained named_scopes?

Take the model:

class Developer < ActiveRecord::Base   named_scope :by_last_name, :order => 'last_name'   named_scope :by_first_name, :order => 'first_name' end

In which order should the following calls return the models?

1) Developer.by_last_name 2) Developer.by_last_name.by_first_name

In case 2) I think that the order should be "last_name, "first_name". This is not the current edge Rails behavior.

Now change the model to:

class Developer < ActiveRecord::Base   default_scope :order => 'salary DESC'   named_scope :by_last_name, :order => 'last_name'   named_scope :by_first_name, :order => 'first_name' end

In which order should the following calls return the models?

1) Developer.find(:all) 2) Developer.by_last_name 3) Developer.by_last_name.by_first_name

In case 2) I think that the order should be "last_name". This is the current edge Rails behavior. In case 3) I think the order should be "last_name, first_name".

Does this match peoples expectations and usage of named_scope and default_scope? If so, I can submit a patch to allow :order options to be merged.

I guess merging order options seems like a good idea but I wouldn't recommend hacking it into with_scope in its current state. Pratik said that we wanted to clean up some of the scoping code after 2.3.1 is released, maybe it's a good idea to tackle order merging during those changes?

It's probably a good idea to create a ticket and submit some testcases, that way we don't forget (:

Manfred

This looks good to me. I’ve been bitten once before by named scopes not merging the order option.

Tekin Suleyman

Ruby on Rails Developer

+44 (0) 161 408 8868

+44 (0) 7968 355 460

http://tekin.co.uk

Thanks everyone for your input. There is now a ticket: http://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/2253-named_scope-and-nested-order-clauses

Erik