# Relationship between tables

**URL:** <https://www.ruby-forum.com/t/relationship-between-tables/118153>\
**Category:** Rails\
**Created:** [October 24, 2007, 10:30am UTC](https://www.ruby-forum.com/t/relationship-between-tables/118153 "2007-10-24T10:30:20Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![remco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9d8465/32.png) [@remco](https://www.ruby-forum.com/u/remco)\
**Post date:** [October 24, 2007, 10:30am UTC](https://www.ruby-forum.com/t/relationship-between-tables/118153/1 "2007-10-24T10:30:20Z")

</div>

Hi,

I have a users table(below) and now i want to create a new table  
“occupation” so the users can fill in there occupation and store it in  
the occupation-table.

Question: how can i realize a realionship between the users-table and  
the occupation-table?

class CreateUsers \< ActiveRecord::Migration  
def self.up  
create\_table :users do |t|  
t.column :username, :string, :limit =\> 64, :null =\> false  
t.column :email, :string, :limit =\> 128, :null =\> false  
t.column :hashed\_password, :string, :limit =\> 64  
t.column :enabled, :boolean, :default =\> true, :null =\> false  
t.column :profile, :text  
t.column :created\_at, :datetime  
t.column :updated\_at, :datetime  
t.column :last\_login\_at, :datetime  
end  
add\_index :users, :username  
end

def self.down  
drop\_table :users  
end  
end

Thanks…

Grtz…remco

---

<div class="post-metadata">

**Author:** ![remco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9d8465/32.png) [@remco](https://www.ruby-forum.com/u/remco)\
**Post date:** [October 24, 2007, 10:44am UTC](https://www.ruby-forum.com/t/relationship-between-tables/118153/2 "2007-10-24T10:44:21Z")

</div>

Remco S. wrote:

> Hi,
> 
> I have a users table(below) and now i want to create a new table  
> “occupation” so the users can fill in there occupation and store it in  
> the occupation-table.
> 
> Question: how can i realize a realionship between the users-table and  
> the occupation-table?
> 
> class CreateUsers \< ActiveRecord::Migration  
> def self.up  
> create\_table :users do |t|  
> t.column :username, :string, :limit =\> 64, :null =\> false  
> t.column :email, :string, :limit =\> 128, :null =\> false  
> t.column :hashed\_password, :string, :limit =\> 64  
> t.column :enabled, :boolean, :default =\> true, :null =\> false  
> t.column :profile, :text  
> t.column :created\_at, :datetime  
> t.column :updated\_at, :datetime  
> t.column :last\_login\_at, :datetime  
> end  
> add\_index :users, :username  
> end
> 
> def self.down  
> drop\_table :users  
> end  
> end
> 
> Thanks…
> 
> Grtz…remco  
> Hi,  
> If a user is having many occupations  
> then you can give like  
> has\_many :occupations in CreateUsers  
> and  
> belongs\_to :occupation,  
> :class\_name =\> “UserOccupation”,  
> :foreign\_key =\> “user\_occupation\_id”

in Occupation table

---

<div class="post-metadata">

**Author:** ![remco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9d8465/32.png) [@remco](https://www.ruby-forum.com/u/remco)\
**Post date:** [October 24, 2007, 10:49am UTC](https://www.ruby-forum.com/t/relationship-between-tables/118153/3 "2007-10-24T10:49:32Z")

</div>

Abhi M. wrote:

> Remco S. wrote:
> 
> > Hi,
> > 
> > I have a users table(below) and now i want to create a new table  
> > “occupation” so the users can fill in there occupation and store it in  
> > the occupation-table.
> > 
> > Question: how can i realize a realionship between the users-table and  
> > the occupation-table?
> > 
> > class CreateUsers \< ActiveRecord::Migration  
> > def self.up  
> > create\_table :users do |t|  
> > t.column :username, :string, :limit =\> 64, :null =\> false  
> > t.column :email, :string, :limit =\> 128, :null =\> false  
> > t.column :hashed\_password, :string, :limit =\> 64  
> > t.column :enabled, :boolean, :default =\> true, :null =\> false  
> > t.column :profile, :text  
> > t.column :created\_at, :datetime  
> > t.column :updated\_at, :datetime  
> > t.column :last\_login\_at, :datetime  
> > end  
> > add\_index :users, :username  
> > end
> > 
> > def self.down  
> > drop\_table :users  
> > end  
> > end
> > 
> > Thanks…
> > 
> > Grtz…remco  
> > Hi,  
> > If a user is having many occupations  
> > then you can give like  
> > has\_many :occupations in CreateUsers  
> > and  
> > belongs\_to :occupation,  
> > :class\_name =\> “UserOccupation”,  
> > :foreign\_key =\> “user\_occupation\_id”
> 
> in Occupation table

Hi,

Thanks for you’re quick reponse…

But must i add a column user\_occupation\_id in the users-table?

remco

---

<div class="post-metadata">

**Author:** ![remco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9d8465/32.png) [@remco](https://www.ruby-forum.com/u/remco)\
**Post date:** [October 24, 2007, 10:59am UTC](https://www.ruby-forum.com/t/relationship-between-tables/118153/4 "2007-10-24T10:59:12Z")

</div>

On 24 Oct 2007, at 09:49, Remco S. wrote:

> > in Occupation table
> 
> Hi,
> 
> Thanks for you’re quick reponse…
> 
> But must i add a column user\_occupation\_id in the users-table?

Possibly. it depends on your data:  
if a user has a single occupation then the user table needs an  
occupation\_id column  
if an occupation belongs to a single user then the occupation table  
needs a user\_id column  
if a user can have many occupations and an occupation can have many  
users then you don’t need to add columns to either of those table,  
but you do need to create a join table occupation\_users with columns  
user\_id and occupation\_id.

You then do  
class User  
has\_many :occupation\_users  
has\_many :occupations, :through =\> :occupation\_users  
end  
and similarly for Occupation.

Fred

---

<div class="post-metadata">

**Author:** ![remco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9d8465/32.png) [@remco](https://www.ruby-forum.com/u/remco)\
**Post date:** [October 24, 2007, 11:00am UTC](https://www.ruby-forum.com/t/relationship-between-tables/118153/5 "2007-10-24T11:00:22Z")

</div>

Remco S. wrote:

> > > Question: how can i realize a realionship between the users-table and  
> > > t.column :created\_at, :datetime
> > 
> > ```
> > :class_name => "UserOccupation",
> > 
> > ```
> 
> remco

Actually, if a user has many occupations, then the relationship will  
need to be modeled as a has\_and\_belongs\_to\_many (HABTM) and you will  
need to create a table called user\_occupations\_users table to model the  
many-to-many relationship. Search for has\_and\_belongs\_to\_many (HABTM)  
and you should be able to get enough to get started.

On the other hand, if a user can belong to only one occupation, then you  
need to add a table called occupations and in your user table, you need  
to occupation\_id as an INT field. You can create a new migration that  
adds the column to the table. Then, user belongs\_to occupation and  
occupation has\_many users.

Cheers,  
Mohit.  
10/24/2007 | 4:58 PM.

---

<div class="post-metadata">

**Author:** ![remco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9d8465/32.png) [@remco](https://www.ruby-forum.com/u/remco)\
**Post date:** [October 24, 2007, 11:10am UTC](https://www.ruby-forum.com/t/relationship-between-tables/118153/6 "2007-10-24T11:10:31Z")

</div>

Hi,  
No that depends on your need  
I just sent you an example  
Thanks

---

<div class="post-metadata">

**Author:** ![remco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9d8465/32.png) [@remco](https://www.ruby-forum.com/u/remco)\
**Post date:** [October 24, 2007, 12:11pm UTC](https://www.ruby-forum.com/t/relationship-between-tables/118153/7 "2007-10-24T12:11:53Z")

</div>

well i think  
we can do it with a direct has\_many relationship between Users and  
occupations , no need of an through relationship if users has\_many users  
only…

But if occupation has many users then we have a many to any  
relationship , in this case we will opt for through relationship .  
DHH
