# Basic question about belongs\_to

**URL:** <https://www.ruby-forum.com/t/basic-question-about-belongs-to/108564>\
**Category:** Rails\
**Created:** [July 4, 2007, 7:09pm UTC](https://www.ruby-forum.com/t/basic-question-about-belongs-to/108564 "2007-07-04T19:09:37Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![surge](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4da419/32.png) [@surge](https://www.ruby-forum.com/u/surge)\
**Post date:** [July 4, 2007, 7:09pm UTC](https://www.ruby-forum.com/t/basic-question-about-belongs-to/108564/1 "2007-07-04T19:09:37Z")

</div>

Hello,

I’ve got three tables:

site {  
id  
name,  
customer\_id  
}

building {  
id,  
site\_id,  
name  
}

equipment {  
id,  
building\_id,  
name  
}

In my building model I have “belongs\_to :site” and in my equipment  
model I have “belongs\_to :building”.

How do I find all pieces of equipment that belong to a particular  
customer?

I tried something like Equipment.find(:all, :include =\>  
[:building, :site], :conditions =\> ‘customer\_id = 1’), but of course  
that doesn’t work because there’s nothing about :site in the equipment  
model. I also tried to put “:include =\> :site” in the “belongs\_to” in  
the building model, but I got an error saying that the association  
named “:site” wasn’t found.

Any ideas?

Thanks!

---

<div class="post-metadata">

**Author:** ![surge](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4da419/32.png) [@surge](https://www.ruby-forum.com/u/surge)\
**Post date:** [July 4, 2007, 8:26pm UTC](https://www.ruby-forum.com/t/basic-question-about-belongs-to/108564/2 "2007-07-04T20:26:42Z")

</div>

> Where’s Customer?  
> I tried to shorten my question, that’s why I didn’t show all the  
> relevant relationships. I do have the customer model.

> Your table names should be plural (e.g., ‘sites’ and ‘buildings’)  
> They are. They aren’t in the example though. Good catch 🙂

> Equipment.find(:all, :include =\> { :building =\> :site }  
> This worked like a charm. I need to read about that variation  
> of :include. Cool!

> (adding “has\_many” associations to mirror your “belongs\_to”; having a  
> Well, so far I haven’t needed to reverse. Is that a no-no not to  
> mirror it?

Thanks Rob. You saved me a lot of searching. Much appreciate.

---

<div class="post-metadata">

**Author:** ![surge](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4da419/32.png) [@surge](https://www.ruby-forum.com/u/surge)\
**Post date:** [July 4, 2007, 7:32pm UTC](https://www.ruby-forum.com/t/basic-question-about-belongs-to/108564/3 "2007-07-04T19:32:38Z")

</div>

On Jul 4, 2007, at 1:08 PM, surge wrote:

> building {

Where’s Customer?

> In my building model I have “belongs\_to :site” and in my equipment  
> model I have “belongs\_to :building”.
> 
> How do I find all pieces of equipment that belong to a particular  
> customer?  
> Do you also have:  
> class Site  
> has\_many :buildings  
> end  
> and so on?  
> Thanks!  
> Your table names should be plural (e.g., ‘sites’ and ‘buildings’)  
> unless you have overridden this with set\_table\_name in your model.

You can also try:

Equipment.find(:all, :include =\> { :building =\> :site },  
:conditions =\> [ ‘site.customer\_id = ?’,  
some\_customer ])

but even if that works as-is, you should consider the other parts  
(adding “has\_many” associations to mirror your “belongs\_to”; having a  
customer model and table; pluralizing your tables) as means to avoid  
future aggravation.

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![surge](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4da419/32.png) [@surge](https://www.ruby-forum.com/u/surge)\
**Post date:** [July 4, 2007, 8:49pm UTC](https://www.ruby-forum.com/t/basic-question-about-belongs-to/108564/4 "2007-07-04T20:49:45Z")

</div>

> Well, so far I haven’t needed to reverse. Is that a no-no not to  
> I meant to say “I haven’t needed the reverse relationship”
