# Table Relationships and ActiveRecord Associations

**URL:** <https://www.ruby-forum.com/t/table-relationships-and-activerecord-associations/53653>\
**Category:** Rails\
**Created:** [February 13, 2006, 8:01am UTC](https://www.ruby-forum.com/t/table-relationships-and-activerecord-associations/53653 "2006-02-13T08:01:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![asetzler](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ac91a4/32.png) [@asetzler](https://www.ruby-forum.com/u/asetzler)\
**Post date:** [February 13, 2006, 8:01am UTC](https://www.ruby-forum.com/t/table-relationships-and-activerecord-associations/53653/1 "2006-02-13T08:01:05Z")

</div>

First of all, I love Ruby on Rails. I have an intermediate  
understanding of PHP, JavaScript, DOM and CSS, and that’s the extent of  
my programming experience. I’m developing an online game’s web presence  
with another colleague. We’re the only two technical guys on a team of  
+10 content developers. So, baring all that in mind, here are my  
questions with a bit of background information:

We have a table for characters. They can have items (weapons, armor,  
etc), skills, spells and other things, like languages. They each have a  
class and race.

My question is about how to set up the different relationships between  
these tables.

Are there any good, in depth tutorials (with extensive examples) on how  
to set up relationships between tables?

For instance, I know that the relationship between a character and its  
class would be:

belongs\_to, has\_many

But how would I set up the relationship between characters and other  
items? If a character has multiple “skills,” how should I set up the  
other tables? Would I use an “in between” table? Would it be easier to  
serialize the information? If so, how does that work?

I’m not terribly concerned about portability. We use DreamHost as our  
host, who is wonderful enough to support Ruby, and we plan to stay with  
them.

I apologize for the gruesomely newbish form of this inquiry, but I would  
love to further the use of Ruby on the Web.

Sincerely,  
Adam

---

<div class="post-metadata">

**Author:** ![asetzler](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ac91a4/32.png) [@asetzler](https://www.ruby-forum.com/u/asetzler)\
**Post date:** [February 13, 2006, 2:03pm UTC](https://www.ruby-forum.com/t/table-relationships-and-activerecord-associations/53653/2 "2006-02-13T14:03:27Z")

</div>

> Are there any good, in depth tutorials (with extensive examples) on how  
> to set up relationships between tables?
> 
> For instance, I know that the relationship between a character and its  
> class would be:
> 
> belongs\_to, has\_many
> 
> But how would I set up the relationship between characters and other  
> items? If a character has multiple “skills,” how should I set up the  
> other tables? Would I use an “in between” table? Would it be easier to  
> serialize the information? If so, how does that work?

I’d suggest starting with by Googling about database normalisation  
techniques.  
What you’re describing isn’t essentionally a RoR issue.

Then purchase a copy of the Pragmatic Programers book and learn how  
Active Record interacts with DB tables.

Finally expect to be doing a fair bit of Googling/posting on why things  
don’t work. I spend yesterday afteroon wondering why my HABTM  
relationships weren’t being created only to find there a bug where a  
model with \>1 HABTM relationships . Its documented and theres a fix but  
I had to search the postings for the answer.

Tony

---

<div class="post-metadata">

**Author:** ![asetzler](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ac91a4/32.png) [@asetzler](https://www.ruby-forum.com/u/asetzler)\
**Post date:** [February 13, 2006, 11:35pm UTC](https://www.ruby-forum.com/t/table-relationships-and-activerecord-associations/53653/3 "2006-02-13T23:35:06Z")

</div>

On Monday, February 13, 2006, at 2:03 PM, Anthony G. wrote:

> > Are there any good, in depth tutorials (with extensive examples) on how  
> > to set up relationships between tables?

> I’d suggest starting with by Googling about database normalisation  
> techniques.  
> What you’re describing isn’t essentionally a RoR issue.

Right. You sound like you need a book about database design in general.  
You can pick up a bit about about databases from reading Rails  
tutorials, but it’s not the whole story. I suggest the book “Database  
Design for Mere Mortals” which is actually a pretty easy read and will  
introduce you to planning your databases, normalization, joins, and all  
that fun stuff.

---

<div class="post-metadata">

**Author:** ![asetzler](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ac91a4/32.png) [@asetzler](https://www.ruby-forum.com/u/asetzler)\
**Post date:** [February 14, 2006, 4:59am UTC](https://www.ruby-forum.com/t/table-relationships-and-activerecord-associations/53653/4 "2006-02-14T04:59:42Z")

</div>

Andrew Otwell wrote:

> On Monday, February 13, 2006, at 2:03 PM, Anthony G. wrote:
> 
> > > Are there any good, in depth tutorials (with extensive examples) on how  
> > > to set up relationships between tables?
> 
> > I’d suggest starting with by Googling about database normalisation  
> > techniques.  
> > What you’re describing isn’t essentionally a RoR issue.
> 
> Right. You sound like you need a book about database design in general.  
> You can pick up a bit about about databases from reading Rails  
> tutorials, but it’s not the whole story. I suggest the book “Database  
> Design for Mere Mortals” which is actually a pretty easy read and will  
> introduce you to planning your databases, normalization, joins, and all  
> that fun stuff.

It may sound strange, but O’Reilly published a book on Microsoft Access  
(of all things) a while ago, and it contained a fairly concise,  
approachable, and detailed treatment of normalization. Since it’s a bit  
dated, you might find a copy for a good price; the chapters on Access  
may not be current, but the database theory sections are still very  
useful.

=Jim

---

<div class="post-metadata">

**Author:** ![asetzler](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ac91a4/32.png) [@asetzler](https://www.ruby-forum.com/u/asetzler)\
**Post date:** [February 15, 2006, 1:04am UTC](https://www.ruby-forum.com/t/table-relationships-and-activerecord-associations/53653/5 "2006-02-15T01:04:55Z")

</div>

Thanks for the advice, everyone who responded to my inquiry! Sounds  
like it’s time to make a trip to B&N!
