# Is this a many to many relationship?

**URL:** <https://www.ruby-forum.com/t/is-this-a-many-to-many-relationship/144685>\
**Category:** Rails\
**Created:** [August 1, 2008, 6:07am UTC](https://www.ruby-forum.com/t/is-this-a-many-to-many-relationship/144685 "2008-08-01T06:07:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![morgan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/0ea827/32.png) [@morgan](https://www.ruby-forum.com/u/morgan)\
**Post date:** [August 1, 2008, 6:07am UTC](https://www.ruby-forum.com/t/is-this-a-many-to-many-relationship/144685/1 "2008-08-01T06:07:55Z")

</div>

So i have one table of data, each data object has two owners from the  
users table.

so it’s

data table

## id owner1\_id owner2\_id

5 1 2  
10 1 2

owner table

## id name

1 bob  
2 cornholio

so when i pull records from the data table for owner1\_id how would i  
access the name field of owner id 2?

---

<div class="post-metadata">

**Author:** ![morgan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/0ea827/32.png) [@morgan](https://www.ruby-forum.com/u/morgan)\
**Post date:** [August 1, 2008, 7:42am UTC](https://www.ruby-forum.com/t/is-this-a-many-to-many-relationship/144685/2 "2008-08-01T07:42:45Z")

</div>

You need three tables to represent a many-to-many association.

class Owner \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :datas  
end

class Data \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :owners  
end

## Table owners:

id: integer  
name: string

## Table datas:

id: integer  
value: integer

## Table datas\_owners:

data\_id: integer  
owner\_id: integer

This is shown using the more basic has\_and\_belongs\_to\_many (HABTM)  
associations. However, in some cases it’s necessary to store  
additional information in the join table managing your many-to-many  
association. In which case your can make the join table into model  
class and use has\_many :through.

Having owner1\_id, owner2\_id is in violation of the “First Normal Form  
(1NF)” relational database rule.

> **[First normal form](https://en.wikipedia.org/wiki/First_normal_form#Repeating_groups)**
>
> First normal form (1NF) is a property of a relation in a relational database. A relation is in first normal form if and only if no attribute domain has relations as elements. Or more informally, that no table column can have tables as values (or no repeating groups). Database normalization is the process of representing a database in terms of relations in standard normal forms, where first normal is a minimal requirement. SQL-92 does not support creating or using table-valued columns, which me In...

On Aug 1, 12:07 am, Morgan M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![morgan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/0ea827/32.png) [@morgan](https://www.ruby-forum.com/u/morgan)\
**Post date:** [August 1, 2008, 6:58pm UTC](https://www.ruby-forum.com/t/is-this-a-many-to-many-relationship/144685/3 "2008-08-01T18:58:07Z")

</div>

Robert W. wrote:

> Having owner1\_id, owner2\_id is in violation of the “First Normal Form  
> (1NF)” relational database rule.
> 
> [First normal form - Wikipedia](http://en.wikipedia.org/wiki/First_normal_form#Repeating_groups)
> 
> On Aug 1, 12:07ï¿½am, Morgan M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

the owner1\_id corresponds to the student associated with the entry.  
the owner2\_id corresponds to the teacher associated with the entry.

i just used the owner1 and 2 for sake of simplicity.

there is a table of students and teachers with a user level to decide  
what they are thats used for basic information and authentication. it  
seemed more logical to treat it that way… please correct me if i’m  
wrong… i’m a little tarded when it comes to database design.
