nested named_scopes and :order

66 views
Skip to first unread message

Erik Andrejko

unread,
Mar 10, 2009, 3:32:01 PM3/10/09
to Ruby on Rails: Core
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.

Manfred Stienstra

unread,
Mar 11, 2009, 5:07:10 AM3/11/09
to rubyonra...@googlegroups.com
On Mar 10, 2009, at 8:32 PM, Erik Andrejko wrote:

>
> 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?

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

Tekin Suleyman

unread,
Mar 11, 2009, 6:15:51 AM3/11/09
to rubyonra...@googlegroups.com
This looks good to me. I've been bitten once before by named scopes not merging the order option.



Erik Andrejko

unread,
Mar 16, 2009, 9:36:59 AM3/16/09
to Ruby on Rails: Core
Reply all
Reply to author
Forward
0 new messages