Forum: Ruby on Rails Single Table Mapping

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
29d82d169c6037d4cbafce0b20fcf04e?d=identicon&s=25 The Barge (thebarge)
on 2006-04-10 16:01
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?
29d82d169c6037d4cbafce0b20fcf04e?d=identicon&s=25 The Barge (thebarge)
on 2006-04-10 16:14
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 Barge 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?
59de94a56fd2c198f33d9515d1c05961?d=identicon&s=25 Tom Mornini (Guest)
on 2006-04-10 17:19
(Received via mailing list)
You need to use has_one instead of has_many

--
-- Tom Mornini
29d82d169c6037d4cbafce0b20fcf04e?d=identicon&s=25 The Barge (thebarge)
on 2006-04-10 17:45
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 Mornini wrote:
> You need to use has_one instead of has_many
>
> --
> -- Tom Mornini
5d15c6821f3c3054c04b85471824ba7c?d=identicon&s=25 Kevin Olbrich (Guest)
on 2006-04-10 17:47
(Received via mailing list)
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 Mornini 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/.
>> _______________________________________________
>> Rails mailing list
>> Rails@lists.rubyonrails.org
>> http://lists.rubyonrails.org/mailman/listinfo/rails
>
>_______________________________________________
>Rails mailing list
>Rails@lists.rubyonrails.org
>http://lists.rubyonrails.org/mailman/listinfo/rails


_Kevin
59de94a56fd2c198f33d9515d1c05961?d=identicon&s=25 Tom Mornini (Guest)
on 2006-04-10 18:03
(Received via mailing list)
On Apr 10, 2006, at 8:19 AM, Tom Mornini 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 Mornini
29d82d169c6037d4cbafce0b20fcf04e?d=identicon&s=25 The Barge (thebarge)
on 2006-04-10 18:23
Kevin Olbrich 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.
This topic is locked and can not be replied to.