Hello. I have what i assume is a simple problem. I have want to be able
to sort a dataset by referencing the text name of a related class.
For example: let’s pretend I have these models: “things” and “colors”.
Things “belongs_to” Colors and Colors “has_one” things.
Thing.color_id = Color.id
So, Thing.color.name = Color.name where Thing.color_id == Color.id
You get it.
So, I want to do this:
mythings = Thing.find(:all, :order => on Thing.color.name)
I want to be able to sort all my things by the colors (red, blue, etc)
by name and not their ID in the database.
On 28 Aug 2008, at 03:27, Joshua A. wrote:
Try this:
mythings = Thing.find(:all, :include => :color, :order => ‘colors.name
asc’)
That will work, but really all you need is :joins. You just need to
join the colours table, not do all the instantiating of the extra
objects.
Fred
Try this:
mythings = Thing.find(:all, :include => :color, :order => ‘colors.name
asc’)
– Josh
Nick Norton wrote:
Hello. I have what i assume is a simple problem. I have want to be able
to sort a dataset by referencing the text name of a related class.
For example: let’s pretend I have these models: “things” and “colors”.
Things “belongs_to” Colors and Colors “has_one” things.
Thing.color_id = Color.id
So, Thing.color.name = Color.name where Thing.color_id == Color.id
You get it.
So, I want to do this:
mythings = Thing.find(:all, :order => on Thing.color.name)
I want to be able to sort all my things by the colors (red, blue, etc)
by name and not their ID in the database.