Forum: Ruby on Rails Named scope and order overriding

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
0502f5733a9e6fa9c169e13115876959?d=identicon&s=25 aa aa (filthysock)
on 2008-11-26 23:55
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?
0502f5733a9e6fa9c169e13115876959?d=identicon&s=25 aa aa (filthysock)
on 2008-11-26 23:56
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"?
This topic is locked and can not be replied to.