# Database relation using RoR

**URL:** <https://www.ruby-forum.com/t/database-relation-using-ror/91911>\
**Category:** Rails\
**Created:** [April 13, 2007, 11:48am UTC](https://www.ruby-forum.com/t/database-relation-using-ror/91911 "2007-04-13T11:48:04Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![darrenevans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/87869e/32.png) [@darrenevans](https://www.ruby-forum.com/u/darrenevans)\
**Post date:** [April 13, 2007, 11:48am UTC](https://www.ruby-forum.com/t/database-relation-using-ror/91911/1 "2007-04-13T11:48:04Z")

</div>

Hi

This is probably quite a simple problem for someone (but not for me!),  
what is the best way to solve the following?

I have 3 tables: users, friends and names. The table formats are (I have  
cut out the irrelevant parts to simplify):

Friends:  
:id :integer  
:user\_id, :integer  
:friend\_identity, :integer

(friend\_identity is a pseudo for the user\_id of the friend)

Users:  
:id :integer

Names:  
:user\_id, :integer  
:firstname, :string  
:lastname, :string

I have the following relations (again only the relevant parts):

class Friend \< ActiveRecord::Base  
belongs\_to :user  
end

class User \< ActiveRecord::Base  
has\_many :friends  
has\_many :names  
end

class Name \< ActiveRecord::Base  
belongs\_to :user  
end

What I would like to do is for all records corresponding to a specific  
user\_id in the friends table, generate a dropdown menu in the view with  
the firstname and the lastname of the user “friend\_identity” and have  
the value equal to their user\_id.

I am guessing that this can be done somehow by relating the name and  
friend tables in RoR (maybe with a foreign\_key??). Is there a simple way  
to do this, Unfortunately, I always end up with a mess of code when I  
try?

Any help would be really appreciated.

---

<div class="post-metadata">

**Author:** ![darrenevans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/87869e/32.png) [@darrenevans](https://www.ruby-forum.com/u/darrenevans)\
**Post date:** [April 13, 2007, 11:51am UTC](https://www.ruby-forum.com/t/database-relation-using-ror/91911/2 "2007-04-13T11:51:45Z")

</div>

Why are names and user\_id in different tables? Surely it makes more  
sense to  
have those all in a Users table as they all relate to one user?

On 4/13/07, Darren E. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Friends:  
> :user\_id, :integer  
> has\_many :friends  
> the value equal to their user\_id.
> 
> >

–  
[http://www.web-buddha.co.uk](http://www.web-buddha.co.uk)

---

<div class="post-metadata">

**Author:** ![darrenevans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/87869e/32.png) [@darrenevans](https://www.ruby-forum.com/u/darrenevans)\
**Post date:** [April 13, 2007, 12:16pm UTC](https://www.ruby-forum.com/t/database-relation-using-ror/91911/3 "2007-04-13T12:16:10Z")

</div>

Dave,

Thanks a lot for your reply. It is an extremely good point that you have  
made!

My original reason for this structure was that users change their names  
over time (the names table also had date fields) & this site covers  
historical records of users. However, what I will do is as you suggest,  
but add a “formally\_known\_as” field where users can add all of their  
former names seperated by a comma. Then my search facility will be able  
to find them.

Because I am still a beginner with RoR, it would be really useful to  
know in principle (very very briefly - without getting into any coding)  
what the solution to my former question would have been.

Many thanks

Darren

Dave G. wrote:

> Why are names and user\_id in different tables? Surely it makes more  
> sense to  
> have those all in a Users table as they all relate to one user?
> 
> On 4/13/07, Darren E. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Friends:  
> > :user\_id, :integer  
> > has\_many :friends  
> > the value equal to their user\_id.
> > 
> > >
> 
> –  
> [http://www.web-buddha.co.uk](http://www.web-buddha.co.uk)

---

<div class="post-metadata">

**Author:** ![darrenevans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/87869e/32.png) [@darrenevans](https://www.ruby-forum.com/u/darrenevans)\
**Post date:** [April 13, 2007, 12:22pm UTC](https://www.ruby-forum.com/t/database-relation-using-ror/91911/4 "2007-04-13T12:22:49Z")

</div>

Sounds like a lot of work to me - how often do users change their first  
and  
last names? A simple update would suffice methinks!

On 4/13/07, Darren E. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> but add a “formally\_known\_as” field where users can add all of their
> 
> > > ```
> > > :user_id, :integer
> > > 
> > > ```
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> >

–  
[http://www.web-buddha.co.uk](http://www.web-buddha.co.uk)

---

<div class="post-metadata">

**Author:** ![darrenevans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/87869e/32.png) [@darrenevans](https://www.ruby-forum.com/u/darrenevans)\
**Post date:** [April 13, 2007, 12:39pm UTC](https://www.ruby-forum.com/t/database-relation-using-ror/91911/5 "2007-04-13T12:39:33Z")

</div>

I’ll take the easy route as you suggest…

Women clearly change their surnames quite often - marriage, divorce  
(sometimes reverting back to maiden names), marriage again sometimes &  
men sometimes too here in Switzerland when they get married!

When finding someone from historical records, changes in names through  
time are very important, but an extra field with muliple entries (I will  
lose the time frame of name changes) and a decent search facility should  
suffice for my needs.

Many thanks again for your help.

Darren

Dave G. wrote:

> Sounds like a lot of work to me - how often do users change their first  
> and  
> last names? A simple update would suffice methinks!
> 
> On 4/13/07, Darren E. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > but add a “formally\_known\_as” field where users can add all of their
> > 
> > > > ```
> > > > :user_id, :integer
> > > > 
> > > > ```
> > 
> > –  
> > Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> > 
> > >
> 
> –  
> [http://www.web-buddha.co.uk](http://www.web-buddha.co.uk)
