# Why doesn't named\_scope sort properly?

**URL:** <https://www.ruby-forum.com/t/why-doesnt-named-scope-sort-properly/149966>\
**Category:** Rails\
**Created:** [October 1, 2008, 8:35pm UTC](https://www.ruby-forum.com/t/why-doesnt-named-scope-sort-properly/149966 "2008-10-01T20:35:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mattclarkson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/958977/32.png) [@mattclarkson](https://www.ruby-forum.com/u/mattclarkson)\
**Post date:** [October 1, 2008, 8:35pm UTC](https://www.ruby-forum.com/t/why-doesnt-named-scope-sort-properly/149966/1 "2008-10-01T20:35:03Z")

</div>

I have this simple named\_scope that returns all Events after a given  
id:

named\_scope :recent\_by\_id, lambda {|id| {  
:conditions =\> [‘id \> ?’, id], :order =\> ‘id ASC’  
}}

The problem is that named\_scope returns a query with an order by  
clause containing created\_at first:  
ORDER BY created\_at DESC, id ASC

How do I get named\_scope to ignore the created\_at and simply give me a  
query sorted by id?

best,

- Matt

---

<div class="post-metadata">

**Author:** ![mattclarkson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/958977/32.png) [@mattclarkson](https://www.ruby-forum.com/u/mattclarkson)\
**Post date:** [October 2, 2008, 12:35am UTC](https://www.ruby-forum.com/t/why-doesnt-named-scope-sort-properly/149966/2 "2008-10-02T00:35:13Z")

</div>

Yes this should work… maybe the other have an idea.

I have a tip though (it doesn’t answer your question):

separate that into two named scopes: recent and by\_id so that you can  
do Model.recent(id).by\_id

More flexible.

Ramon T.

---

<div class="post-metadata">

**Author:** ![mattclarkson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/958977/32.png) [@mattclarkson](https://www.ruby-forum.com/u/mattclarkson)\
**Post date:** [October 3, 2008, 9:51pm UTC](https://www.ruby-forum.com/t/why-doesnt-named-scope-sort-properly/149966/3 "2008-10-03T21:51:08Z")

</div>

I just created a super simple prototype app to test this out:

I created a Post model:

class Post \< ActiveRecord::Base  
named\_scope :recent\_by\_id, lambda { |id| {  
:conditions =\> [‘id \> ?’, id], :order =\> ‘id ASC’ } }  
end

$ ./script/console  
Loading development environment (Rails 2.1.1)

> > posts = Post.recent\_by\_id(1)

Development Log:  
SELECT \* FROM “posts” WHERE (id \> 1) ORDER BY id ASC

Are you sure you are not including the :order somewhere else? This seems  
to work as expected.

Matt C. wrote:

> I have this simple named\_scope that returns all Events after a given  
> id:
> 
> named\_scope :recent\_by\_id, lambda {|id| {  
> :conditions =\> [‘id \> ?’, id], :order =\> ‘id ASC’  
> }}
> 
> The problem is that named\_scope returns a query with an order by  
> clause containing created\_at first:  
> ORDER BY created\_at DESC, id ASC
> 
> How do I get named\_scope to ignore the created\_at and simply give me a  
> query sorted by id?
> 
> best,
> 
> - Matt
