I’ve had a number of occasions now where I want to find something in my
model that is not on the primary object. For instance I’ve got a class
Alert that has_one Status. Status has a “label”.
I want to find all of my Alert objects and order the results
alphabetically by status label.
Can I do that without going into SQL? Right now I find myself doing an
Alert.find :all, :joins=>“as a join status s on a.status_id=s.id”,
:order=>“label” (that’s from memory, sorry if there’s any syntax
errors)…
You are correct, I meant “belongs_to” – there are a fixed number of
Status objects, each with unique ID, and a variable number of Alert
objects each of which points to one of them.
I see that :include is probably what I should be using in situations
like this.
Knew there had to be something :).
Thanks!
D
Brian H. wrote:
This is all possible with associations.
Are you sure Alert has_one status? This implies that the alerts table
does not have a status_id column.
If it does, then you want belongs_to :status
Now, this is easy if status has a field called label