# Are the find\_ methods strictly a SQL thing?

**URL:** <https://www.ruby-forum.com/t/are-the-find-methods-strictly-a-sql-thing/95637>\
**Category:** Rails\
**Created:** [June 7, 2006, 9:54pm UTC](https://www.ruby-forum.com/t/are-the-find-methods-strictly-a-sql-thing/95637 "2006-06-07T21:54:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Duane](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b4bc9f/32.png) [@Duane](https://www.ruby-forum.com/u/Duane)\
**Post date:** [June 7, 2006, 9:54pm UTC](https://www.ruby-forum.com/t/are-the-find-methods-strictly-a-sql-thing/95637/1 "2006-06-07T21:54:26Z")

</div>

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)…

but there’s gotta be a better way?

---

<div class="post-metadata">

**Author:** ![Duane](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b4bc9f/32.png) [@Duane](https://www.ruby-forum.com/u/Duane)\
**Post date:** [June 7, 2006, 10:20pm UTC](https://www.ruby-forum.com/t/are-the-find-methods-strictly-a-sql-thing/95637/2 "2006-06-07T22:20:05Z")

</div>

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

@alerts = Alert.find :all, :include =\>:status, :order =\> ‘label asc’

If Status has a label\_id column (and there is a Label model, and  
Status belongs\_to :label) then you’ll have to do a little more work

And by a little, I mean hardly any

@alerts = Alert.find :all, :include =\>[{:status =\> :label]}, :order =\>  
‘label asc’

This is untested, so if any of this is buggy, let me know and I’ll help  
fix it.

---

<div class="post-metadata">

**Author:** ![Duane](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b4bc9f/32.png) [@Duane](https://www.ruby-forum.com/u/Duane)\
**Post date:** [June 8, 2006, 2:25am UTC](https://www.ruby-forum.com/t/are-the-find-methods-strictly-a-sql-thing/95637/3 "2006-06-08T02:25:48Z")

</div>

Hi Brian,

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
> 
> @alerts = Alert.find :all, :include =\>:status, :order =\> ‘label asc’
> 
> If Status has a label\_id column (and there is a Label model, and  
> Status belongs\_to :label) then you’ll have to do a little more work
> 
> And by a little, I mean hardly any
> 
> @alerts = Alert.find :all, :include =\>[{:status =\> :label]}, :order =\>  
> ‘label asc’
> 
> This is untested, so if any of this is buggy, let me know and I’ll help  
> fix it.
