# ActiveRecord associations

**URL:** <https://www.ruby-forum.com/t/activerecord-associations/60246>\
**Category:** Rails\
**Created:** [May 11, 2006, 4:09pm UTC](https://www.ruby-forum.com/t/activerecord-associations/60246 "2006-05-11T16:09:41Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![rrwhite](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@rrwhite](https://www.ruby-forum.com/u/rrwhite)\
**Post date:** [May 11, 2006, 4:09pm UTC](https://www.ruby-forum.com/t/activerecord-associations/60246/1 "2006-05-11T16:09:41Z")

</div>

Can someone clarify this for me? Suppose I have two tables like:

class Company \< ActiveRecord::Base  
belongs\_to :category  
end

class Category \< ActiveRecord::Base  
has\_many :companies  
end

I understand that the belongs\_to causes related companies to be read  
from the db when I ask for a category. I suspect SQL something like

select \* from categories, companies  
where category.id = myid  
and companies.category\_id = category.id

but what SQL results from the has\_many?

---

<div class="post-metadata">

**Author:** ![rrwhite](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@rrwhite](https://www.ruby-forum.com/u/rrwhite)\
**Post date:** [May 11, 2006, 4:15pm UTC](https://www.ruby-forum.com/t/activerecord-associations/60246/2 "2006-05-11T16:15:50Z")

</div>

You can look at the development.log file to see all the SQL queries made  
but it’s probably like the query you said:

belongs\_to:  
select \* from categories where category\_id = {company\_category\_id}

- where category id is retrived from the company results

has\_many:  
select \* from companies where category\_id = {category\_id}

- where category id is the id of the retrived category

Steve

---

<div class="post-metadata">

**Author:** ![rrwhite](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@rrwhite](https://www.ruby-forum.com/u/rrwhite)\
**Post date:** [May 11, 2006, 4:28pm UTC](https://www.ruby-forum.com/t/activerecord-associations/60246/3 "2006-05-11T16:28:45Z")

</div>

On May 11, 2006, at 10:08 AM, Richard W. wrote:

> I understand that the belongs\_to causes related companies to be read  
> from the db when I ask for a category. I suspect SQL something like
> 
> select \* from categories, companies  
> where category.id = myid  
> and companies.category\_id = category.id
> 
> but what SQL results from the has\_many?

I would say that you are thinking about this in the wrong way.  
Technically the belong\_to and the has\_many methods don’t specifically  
create SQL. What they do is create a programatic link between the two  
models that is then expressed as SQL when you request specific data  
from the database.

If you do: Company.find(:first), say through script/console, SQL is  
generated, but no SQL specific to the belongs\_to relationship.

If you do: Company.find(:first).category, you actually create two SQL  
requests to the database. The first is to return the first Company  
record. The second is to return the parent Category record of that  
Company record.

Likewise, if you do: Category.find(:first).companies, you get 2 SQL  
requests, the second of which brings back an array of child Company  
records.

If you want a deeper understanding of this, there are several things  
you should do. First, I recommend getting the Agile Web D.  
with Rails book ([http://pragmaticprogrammer.com/](http://pragmaticprogrammer.com/)).

Second, you should read through the API docs on this page:  
\<[Peak Obsession](http://rails.rubyonrails.com/classes/ActiveRecord/Associations/)  
ClassMethods.html\>

Third, you should launch your Rails app under Development, with the  
ability to see the WEBrick/lighttpd output, launch script/console and  
give some find calls a try, looking at the web server output. In that  
output will be the SQL generated by Rails.

-Brian

---

<div class="post-metadata">

**Author:** ![rrwhite](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@rrwhite](https://www.ruby-forum.com/u/rrwhite)\
**Post date:** [May 11, 2006, 4:38pm UTC](https://www.ruby-forum.com/t/activerecord-associations/60246/4 "2006-05-11T16:38:43Z")

</div>

On May 11, 2006, at 10:29 AM, Richard W. wrote:

> > select \* from companies where category\_id = {category\_id}
> > 
> > - where category id is the id of the retrived category
> 
> So I guess that if you don’t ever want to do the has\_many query, i.e.  
> you don’t every want to get a list of categories for a particular  
> set of  
> companies you would not need to have the has\_many association in  
> the model.

Not true, IMO. Unfortunately, while Stephen’s description is  
essentially accurate, it’s too simplistic a view into what  
ActiveRecord associations can do for you. If you have a relationship  
where it makes sense to say one model belongs\_to another model, the  
“parent” model should have the appropriate has\_\* association  
specified within it.

I believe it would technically work, if you only specified one side  
of the association, you would be cutting off half (or more) of the  
functionality that ActiveRecord is trying to provide. I would say  
it’s actually irrelevant if the code you are currently planning to  
develop only accesses the association from one side, you want to have  
both sides specified so that when you need both sides, it will be there.

-Brian

---

<div class="post-metadata">

**Author:** ![rrwhite](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@rrwhite](https://www.ruby-forum.com/u/rrwhite)\
**Post date:** [May 11, 2006, 4:31pm UTC](https://www.ruby-forum.com/t/activerecord-associations/60246/5 "2006-05-11T16:31:56Z")

</div>

On Thursday, May 11, 2006, at 3:15 PM, Stephen B. wrote:

> > class Category \< ActiveRecord::Base  
> > but what SQL results from the has\_many?
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

So I guess that if you don’t ever want to do the has\_many query, i.e.  
you don’t every want to get a list of categories for a particular set of  
companies you would not need to have the has\_many association in the  
model.

---

<div class="post-metadata">

**Author:** ![rrwhite](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@rrwhite](https://www.ruby-forum.com/u/rrwhite)\
**Post date:** [May 11, 2006, 4:38pm UTC](https://www.ruby-forum.com/t/activerecord-associations/60246/6 "2006-05-11T16:38:52Z")

</div>

As Brian said, has\_many defines a relationship - it doesn’t generate  
SQL. There is more involved in this relationship that just selecting out  
children, such as creating and deleting related records.

Also, the query to select children wont be run unless you try to do  
something with them:

for company in @category.companies

# … code

end

I also second all of Brian’s advice.

Cheers,

Steve

---

<div class="post-metadata">

**Author:** ![rrwhite](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@rrwhite](https://www.ruby-forum.com/u/rrwhite)\
**Post date:** [May 11, 2006, 4:39pm UTC](https://www.ruby-forum.com/t/activerecord-associations/60246/7 "2006-05-11T16:39:00Z")

</div>

Thanks Brian.

What I was confused about was that I _never_ see example model where  
ONLY a has\_many or ONLY a belongs to is shown. Always they are shown as  
a pair. I wanted to know if it is “normal” to use only one or the other  
and when that would be.

I think I understand now that you can use either one independently of  
the other or you can use both together as required by the application.
