# Named scope and order overriding

**URL:** <https://www.ruby-forum.com/t/named-scope-and-order-overriding/154262>\
**Category:** Rails\
**Created:** [November 26, 2008, 11:55pm UTC](https://www.ruby-forum.com/t/named-scope-and-order-overriding/154262 "2008-11-26T23:55:17Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![deadinplastic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/d2c977/32.png) [@deadinplastic](https://www.ruby-forum.com/u/deadinplastic)\
**Post date:** [November 26, 2008, 11:55pm UTC](https://www.ruby-forum.com/t/named-scope-and-order-overriding/154262/1 "2008-11-26T23:55:17Z")

</div>

Hi everyone,  
My question is: How do I override named\_scope settings rather than merge  
with them?

Say I have a model Vehicles  
It has a named\_scope called red\_trucks that sets some filters and an  
order by id.

Now lets say I want to find the unique values for an attribute.  
So I might go

Vehicles.red\_trucks.find(:all, “SELECT DISTINCT model\_year”)  
This breaks in postgres with the following error  
“for SELECT DISTINCT, ORDER BY expressions must appear in select list”

ok, so lets add the model\_year  
Vehicles.red\_trucks.find(:all, “SELECT DISTINCT model\_year”,  
:order=\>“model\_year”)

This still gives me the same error because the ‘order by id’ is still  
there from the red\_trucks name\_scope. How do I override the order so it  
because “model\_year” instead of “model\_year,id”?

Or is there a better way of doing this?

---

<div class="post-metadata">

**Author:** ![deadinplastic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/d2c977/32.png) [@deadinplastic](https://www.ruby-forum.com/u/deadinplastic)\
**Post date:** [November 26, 2008, 11:56pm UTC](https://www.ruby-forum.com/t/named-scope-and-order-overriding/154262/2 "2008-11-26T23:56:05Z")

</div>

aa aa wrote:  
small typo

> This still gives me the same error because the ‘order by id’ is still  
> there from the red\_trucks name\_scope. How do I override the order so it  
> because “model\_year” instead of “model\_year,id”?  
> should be  
> This still gives me the same error because the ‘order by id’ is still  
> there from the red\_trucks name\_scope. How do I override the order so it  
> is “model\_year” instead of “model\_year,id”?
