# Joining/referencing tables

**URL:** <https://www.ruby-forum.com/t/joining-referencing-tables/108895>\
**Category:** Rails\
**Created:** [July 9, 2007, 4:19am UTC](https://www.ruby-forum.com/t/joining-referencing-tables/108895 "2007-07-09T04:19:43Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![dark-omen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c68b51/32.png) [@dark-omen](https://www.ruby-forum.com/u/dark-omen)\
**Post date:** [July 9, 2007, 4:19am UTC](https://www.ruby-forum.com/t/joining-referencing-tables/108895/1 "2007-07-09T04:19:43Z")

</div>

I am wondering if any one could explain table relationships for me using  
rails. What I am trying to do requires the joining of a product and the  
comments for the product, with the user information of whom posted the  
comment. So I have a products table that references a comments table  
that is referenced by the product\_id and the product\_type, and the  
comments table gets user information from the users table by a user\_id.  
So right now, I am familiar with has\_ and belongs\_to, but  
don’t know how to make it work with what I am trying to do, and thus the  
further explanation.

---

<div class="post-metadata">

**Author:** ![dark-omen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c68b51/32.png) [@dark-omen](https://www.ruby-forum.com/u/dark-omen)\
**Post date:** [July 10, 2007, 1:20am UTC](https://www.ruby-forum.com/t/joining-referencing-tables/108895/2 "2007-07-10T01:20:25Z")

</div>

Thanks for all of that. But how would I create relationships where i use  
a foreign key in the has\_many and belongs\_to declarations? So for  
instance if I automatically wanted to return all the comments and the  
users of the comments without having to use the :include parameter?

---

<div class="post-metadata">

**Author:** ![dark-omen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c68b51/32.png) [@dark-omen](https://www.ruby-forum.com/u/dark-omen)\
**Post date:** [July 10, 2007, 1:56am UTC](https://www.ruby-forum.com/t/joining-referencing-tables/108895/3 "2007-07-10T01:56:11Z")

</div>

On 7/9/07, Scott Pn [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Thanks for all of that. But how would I create relationships where i use  
> a foreign key in the has\_many and belongs\_to declarations? So for  
> instance if I automatically wanted to return all the comments and the  
> users of the comments without having to use the :include parameter?

I’m not sure what you mean, honestly. has\_many and belongs\_to rely on  
there being a foreign key. The comments table would have a product\_id  
foreign key to products, as well as a user\_id key to users. Having  
those keys and the corresponding has\_many and belongs\_to statements  
set up the relationship to be used programmatically.

I don’t know how you would automatically load the associations without  
:include. Why do you want to?

The best I think you could do is to write a new finder or override the  
default finder, this way you can encapsulate the call.

class Product \< ActiveRecord::Base  
def self.heavy\_find(id, options = {})  
find(id, { :include =\> [:comments =\> :users] }.merge(options))  
end

OR

def self.find(id, options = {})  
super(id, { :include =\> [:comments =\> :users] }.merge(options))  
end  
end

_shrug_

Pat

---

<div class="post-metadata">

**Author:** ![dark-omen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c68b51/32.png) [@dark-omen](https://www.ruby-forum.com/u/dark-omen)\
**Post date:** [July 9, 2007, 10:37am UTC](https://www.ruby-forum.com/t/joining-referencing-tables/108895/4 "2007-07-09T10:37:33Z")

</div>

On 7/8/07, Scott Pn [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> >

Hey Scott,

That’s a pretty easy set up. You won’t have to do any joins, Rails  
will take care of everything for you 🙂

Anyway, now that you’ve got the tables set up, you can use AR’s has\_\*  
and belongs\_to methods to set up the associations.

class Product  
has\_many :comments  
end

class Comment  
belongs\_to :user  
end

You can set up the reverse associations if you want/need to.

This code will let you do something like:

p = Product.find 1  
p.comments.first.user.username # ‘dopey’

comments of course is the collection of all comments, and for each  
comment you have access to the user object.

Now one problem is that if you have 1 product with 10 comments, you’ll  
end up making 21 db calls. 1 for the product itself, 10 for each of  
the comments, and 10 for the associated user. Fortunately Rails lets  
you eager load the assocations.

p = Product.find 1, :include =\> :comments

will take care of the join for you to include comments, so they’re  
fetched along with the product. You can also go as deep as you need:

p = Product.find 1, :include =\> [:comments =\> :user]

This will fetch all the data you need in one query.

Pat

---

<div class="post-metadata">

**Author:** ![dark-omen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c68b51/32.png) [@dark-omen](https://www.ruby-forum.com/u/dark-omen)\
**Post date:** [July 10, 2007, 3:51am UTC](https://www.ruby-forum.com/t/joining-referencing-tables/108895/5 "2007-07-10T03:51:15Z")

</div>

Pat M. wrote:

> On 7/9/07, Scott Pn [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > 
> 
> Funny, [http://agilewebdevelopment.com/plugins/includebydefault](http://agilewebdevelopment.com/plugins/includebydefault) was  
> _just_ published to the rails plugins repository. Looks like it ought  
> to do exactly what you want.
> 
> Pat

o, awesome, thanks for the find.

---

<div class="post-metadata">

**Author:** ![dark-omen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c68b51/32.png) [@dark-omen](https://www.ruby-forum.com/u/dark-omen)\
**Post date:** [July 10, 2007, 3:29am UTC](https://www.ruby-forum.com/t/joining-referencing-tables/108895/6 "2007-07-10T03:29:16Z")

</div>

On 7/9/07, Scott Pn [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 

Funny, [http://agilewebdevelopment.com/plugins/includebydefault](http://agilewebdevelopment.com/plugins/includebydefault) was  
_just_ published to the rails plugins repository. Looks like it ought  
to do exactly what you want.

Pat
