Question: I'm trying to get a specific order from an associated table. Here's my setup:

I have a trip which has triplocations added to it. The Triplocation table has a column which defines the order of the locations with a column named location\_order. Now i'd like to order on this column when i collect my single trip in the show method.

I've tried `@trip = Trip.find(params[:id], :order=> 'triplocations.location_order')` but got the following error:

```
PG::Error: ERROR:  missing FROM-clause entry for table "triplocations" LINE 1: ....* FROM "trips"  WHERE "trips"."id" = $1 ORDER BY triplocati...

```

Any ideas how to get my location\_order ordered?

Thanks in advance!

Answer: 
It sounds like you want the triplocations association to be ordered, i.e. that `@trip.trip_locations` is ordered by the location order. 

I'd suggest adding a default scope to `TripLocation`:

```
default_scope :order => 'location_order DESC'

```
First, find is only going to give you one record, so you either want to use where or find all.

Second, if you want to return TripLocations, and not trips, you need to start your query with that

Try this:

```
TripLocations.where(:trip_id => params[:id]).order(:location_order)

```

If you want to order the locations on a trip that you have, you can do that through the associations in your model as well

```
# load the trip
@trip = Trip.find(params[:id])

# order the locations
@locations = @trip.triplocations.order(:location_order)

```