# How to specify relationship between these models

**URL:** <https://www.ruby-forum.com/t/how-to-specify-relationship-between-these-models/185401>\
**Category:** Rails\
**Created:** [March 19, 2010, 9:38am UTC](https://www.ruby-forum.com/t/how-to-specify-relationship-between-these-models/185401 "2010-03-19T09:38:01Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![tom-mac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/bc8723/32.png) [@tom-mac](https://www.ruby-forum.com/u/tom-mac)\
**Post date:** [March 19, 2010, 9:38am UTC](https://www.ruby-forum.com/t/how-to-specify-relationship-between-these-models/185401/1 "2010-03-19T09:38:01Z")

</div>

Hi  
I have a user model which I used as authlogic. What I need to  
implement is I have to add staff also to the system. And I designed  
tables like

## users

name  
email  
phone etc

## staffs

city  
state  
user\_id  
My question is is this the right approach? And if yes what  
would be the relation from users to staffs? I think we can say

staff belongs\_to user  
But what is the reverse?

Thanks in advance  
Tom

---

<div class="post-metadata">

**Author:** ![tom-mac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/bc8723/32.png) [@tom-mac](https://www.ruby-forum.com/u/tom-mac)\
**Post date:** [March 19, 2010, 9:55am UTC](https://www.ruby-forum.com/t/how-to-specify-relationship-between-these-models/185401/2 "2010-03-19T09:55:56Z")

</div>

Let say

you are having staff, directors and managers as part of your system, and  
their details will be in users table so let say we can build inheritance  
based relationship like below:

Class User \< ActiveRecord::Base  
end

Class Staff \< User  
end

Class Director \< User  
end

Class Manager \< User  
end

users table:  
id  
type  
name  
email  
phone  
city  
state etc

Now just you can create staff, directors as follows

staff = Staff.create(:name=\>“Tom M.”, :email=\>“removed\_email\_address@domain.invalid”)

So it will create one record in users table and mark the column type  
with “Staff”. Hope it will solve your issue.

Regards.  
T.Veeraa

---

<div class="post-metadata">

**Author:** ![tom-mac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/bc8723/32.png) [@tom-mac](https://www.ruby-forum.com/u/tom-mac)\
**Post date:** [March 19, 2010, 12:43pm UTC](https://www.ruby-forum.com/t/how-to-specify-relationship-between-these-models/185401/3 "2010-03-19T12:43:35Z")

</div>

Hi  
Thanks for your reply. This I have already thought of. But one  
thing I can’t understand is why everything is stored in a single table  
say users here? Can we store each items for say staff,director etc to  
there own tables? Or Am I wrong?

Tom

---

<div class="post-metadata">

**Author:** ![tom-mac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/bc8723/32.png) [@tom-mac](https://www.ruby-forum.com/u/tom-mac)\
**Post date:** [March 19, 2010, 1:16pm UTC](https://www.ruby-forum.com/t/how-to-specify-relationship-between-these-models/185401/4 "2010-03-19T13:16:28Z")

</div>

On 19 March 2010 11:43, Tom M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> why everything is stored in a single table

That’s called Single Table Inheritance (STI)

> Can we store each items for say staff,director etc to  
> there own tables? Or Am I wrong?

You’re not wrong; you can do it with different tables if you want.

Data modelling is done however is best for you. Sometimes you might  
start with STI, refactor to associations, and then refactor back to  
STI as your project requirements evolve.  
Problems may occur when, for instance, your Managers also need to have  
elements of the properties of Staff - you can’t inherit from two  
base-classes, so you have to start looking at mixing-in modules  
instead, or of some other method of structuring your data to make it  
make sense. Also, what happens if you have someone who’s a staff  
member of two offices (if that’s possible for your organisation)? Can  
they have different roles in the different places? etc…

Personally, I tend to keep “users” as a table for  
authentication-related info, and in my “people” table (I’d tend to  
avoid STI for people) I keep personal info, link to a user record, and  
assign a “role” which is used for handling permissions. I’d rather  
have lots of associations for data, because that’s more flexible than  
one wide flat table (the likes of which STI generally leads to…)

---

<div class="post-metadata">

**Author:** ![tom-mac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/bc8723/32.png) [@tom-mac](https://www.ruby-forum.com/u/tom-mac)\
**Post date:** [March 19, 2010, 1:54pm UTC](https://www.ruby-forum.com/t/how-to-specify-relationship-between-these-models/185401/5 "2010-03-19T13:54:31Z")

</div>

> ```
> My question is is this the right approach? And if yes what
> 
> ```
> 
> would be the relation from users to staffs? I think we can say
> 
> staff belongs\_to user  
> But what is the reverse?

user has\_one staff

Cheers,

Andy

---

<div class="post-metadata">

**Author:** ![tom-mac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/bc8723/32.png) [@tom-mac](https://www.ruby-forum.com/u/tom-mac)\
**Post date:** [March 22, 2010, 5:07am UTC](https://www.ruby-forum.com/t/how-to-specify-relationship-between-these-models/185401/6 "2010-03-22T05:07:00Z")

</div>

Hi  
Lots of thanks for all your reply. What I am finally doing is  
following. (Is it you people suggested?). I have still problem in  
creating the view for adding a staff. Please correct me if what I am  
doing is wrong way.

## users

name  
email  
phone etc

## staffs

city  
state  
user\_id

Now in staff controller  
--------------------  
def new  
@user = User.new  
end

## staff/new.html.erb

\<% form\_for @user,:url =\> staff\_index\_url, do |u| %\>

\<%= u.text\_field :first\_name,:maxlength =\> 50 %\>

 \<% u.fields\_for :staff do |s| %\> \<%= s.text\_field :address\_street1,:maxlength =\> 50 %\> \<%end%\> \<%= submit\_tag "ok"%\> \<%end%\>

Thanks  
Tom

---

<div class="post-metadata">

**Author:** ![tom-mac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/bc8723/32.png) [@tom-mac](https://www.ruby-forum.com/u/tom-mac)\
**Post date:** [March 19, 2010, 1:44pm UTC](https://www.ruby-forum.com/t/how-to-specify-relationship-between-these-models/185401/7 "2010-03-19T13:44:23Z")

</div>

Michael P. wrote:

> Personally, I tend to keep “users” as a table for  
> authentication-related info, and in my “people” table (I’d tend to  
> avoid STI for people) I keep personal info, link to a user record, and  
> assign a “role” which is used for handling permissions. I’d rather  
> have lots of associations for data, because that’s more flexible than  
> one wide flat table (the likes of which STI generally leads to…)

+! for Michael’s view.

User, Manager, Staff, Guest, whatever - they’re all users of your  
system. The core definition of a user, and all those attributes can go  
into a base Users table.

If there are unique attributes for some other ‘refinements’ of User, I  
tend to push those attributes out to a related table for the type,  
allowing a single User entity to be more than one flavor of user. The  
Guest, being a generic User, doesn’t have any relationships.

Just one of many possible solutions, but this type of User modeling has  
survived many projects.
