# Single Table Mapping

**URL:** <https://www.ruby-forum.com/t/single-table-mapping/57386>\
**Category:** Rails\
**Created:** [April 10, 2006, 4:01pm UTC](https://www.ruby-forum.com/t/single-table-mapping/57386 "2006-04-10T16:01:23Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![thebarge](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b38774/32.png) [@thebarge](https://www.ruby-forum.com/u/thebarge)\
**Post date:** [April 10, 2006, 4:01pm UTC](https://www.ruby-forum.com/t/single-table-mapping/57386/1 "2006-04-10T16:01:23Z")

</div>

This is pretty simple, but I must be missing the obvious. I’ve got a  
table that’s similar to this:

USERS:

- id int
- username varchar(64)
- password varchar(64)
- role\_id int

ROLES:

- id int
- description varchar(16)

I’ve had no troubles doing has\_many relationships at all. But I want to  
map the user.role directly to the role model automatically (which sounds  
like it should be really simple). Right now if I look at the user’s  
attributes the correct role\_id is shown, but I get a no method error if  
I try and access user.role.

Do I need to do anything to tell rails to create a role model for the  
user?

---

<div class="post-metadata">

**Author:** ![thebarge](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b38774/32.png) [@thebarge](https://www.ruby-forum.com/u/thebarge)\
**Post date:** [April 10, 2006, 5:19pm UTC](https://www.ruby-forum.com/t/single-table-mapping/57386/2 "2006-04-10T17:19:47Z")

</div>

You need to use has\_one instead of has\_many

–  
– Tom M.

---

<div class="post-metadata">

**Author:** ![thebarge](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b38774/32.png) [@thebarge](https://www.ruby-forum.com/u/thebarge)\
**Post date:** [April 10, 2006, 4:14pm UTC](https://www.ruby-forum.com/t/single-table-mapping/57386/3 "2006-04-10T16:14:45Z")

</div>

OK, well I got it to work by changing the table format and removing the  
role\_id from the USERS table and adding a user\_id to the ROLES table  
then adding a has\_one :role to the user model.

But is there not a way for it to map by using the role\_id in the USERS  
table? Since its a one-to-one relationship I don’t see the point of  
having a row in the ROLES table for each user when all I really need is  
a single column in the USERS table.

The B. wrote:

> This is pretty simple, but I must be missing the obvious. I’ve got a  
> table that’s similar to this:
> 
> USERS:
> 
> - id int
> - username varchar(64)
> - password varchar(64)
> - role\_id int
> 
> ROLES:
> 
> - id int
> - description varchar(16)
> 
> I’ve had no troubles doing has\_many relationships at all. But I want to  
> map the user.role directly to the role model automatically (which sounds  
> like it should be really simple). Right now if I look at the user’s  
> attributes the correct role\_id is shown, but I get a no method error if  
> I try and access user.role.
> 
> Do I need to do anything to tell rails to create a role model for the  
> user?

---

<div class="post-metadata">

**Author:** ![thebarge](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b38774/32.png) [@thebarge](https://www.ruby-forum.com/u/thebarge)\
**Post date:** [April 10, 2006, 5:45pm UTC](https://www.ruby-forum.com/t/single-table-mapping/57386/4 "2006-04-10T17:45:07Z")

</div>

Yeah that’s how I ended up fixing the problem, but has\_one requires a  
user\_id field in the ROLES table. This means for each user in the  
database there is a row in the ROLES table. Since this is one-to-one it  
would be better (IMHO) to have a single ROLE\_ID in the USERS table, and  
only have one row per role in the ROLES table. This would mean just one  
additional smallint column the the USERS table and the ROLES table would  
be nice and small and wouldn’t grow (unless I added more roles).

Tom M. wrote:

> You need to use has\_one instead of has\_many
> 
> –  
> – Tom M.

---

<div class="post-metadata">

**Author:** ![thebarge](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b38774/32.png) [@thebarge](https://www.ruby-forum.com/u/thebarge)\
**Post date:** [April 10, 2006, 5:47pm UTC](https://www.ruby-forum.com/t/single-table-mapping/57386/5 "2006-04-10T17:47:47Z")

</div>

When you set up your associations, make the one that you want to have  
the foreign key ‘belongs\_to’ the one it is associated with.

In your case,

user\_table  
:id  
end

role\_table  
:user\_id  
end

then your user should ‘belongs\_to :role’ and role should ‘has\_many  
:users’ or ‘has\_one :user’

If each user can have one role, but multiple users can have the same  
role, then you need a ‘has\_many’ for the role.

On Monday, April 10, 2006, at 8:19 AM, Tom M. wrote:

> > But is there not a way for it to map by using the role\_id in the USERS
> > 
> > > - username varchar(64)
> > > 
> > > > > sounds  
> > > > > Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> > 
> > * * *
> > 
> > Rails mailing list  
> > removed\_email\_address@domain.invalid  
> > [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

\_Kevin

---

<div class="post-metadata">

**Author:** ![thebarge](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b38774/32.png) [@thebarge](https://www.ruby-forum.com/u/thebarge)\
**Post date:** [April 10, 2006, 6:23pm UTC](https://www.ruby-forum.com/t/single-table-mapping/57386/6 "2006-04-10T18:23:04Z")

</div>

Kevin O. wrote:

> When you set up your associations, make the one that you want to have  
> the foreign key ‘belongs\_to’ the one it is associated with.
> 
> In your case,
> 
> user\_table  
> :id  
> end
> 
> role\_table  
> :user\_id  
> end
> 
> then your user should ‘belongs\_to :role’ and role should ‘has\_many  
> :users’ or ‘has\_one :user’
> 
> If each user can have one role, but multiple users can have the same  
> role, then you need a ‘has\_many’ for the role.

Hah, I knew it would be something simple. I typo’d and had the  
belongs\_to and has\_many in the wrong models. Thanks.

---

<div class="post-metadata">

**Author:** ![thebarge](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b38774/32.png) [@thebarge](https://www.ruby-forum.com/u/thebarge)\
**Post date:** [April 10, 2006, 6:03pm UTC](https://www.ruby-forum.com/t/single-table-mapping/57386/7 "2006-04-10T18:03:08Z")

</div>

On Apr 10, 2006, at 8:19 AM, Tom M. wrote:

> > having a row in the ROLES table for each user when all I really
> > 
> > > - username varchar(64)  
> > > sounds  
> > > like it should be really simple). Right now if I look at the user’s  
> > > attributes the correct role\_id is shown, but I get a no method  
> > > error if  
> > > I try and access user.role.
> > > 
> > > Do I need to do anything to tell rails to create a role model for  
> > > the  
> > > user?
> 
> You need to use has\_one instead of has\_many

Oops, my advice was wrong.

You want belongs\_to.

–  
– Tom M.
