Forum: Ruby on Rails database relation using RoR

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.
Darren E. (Guest)
on 2007-04-13 13:48
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.
Dave G. (Guest)
on 2007-04-13 13:51
(Received via mailing list)
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> wrote:
> Friends:
>     :user_id,          :integer
>    has_many :friends
> the value equal to their user_id.
>
> >
>


--
http://www.web-buddha.co.uk
Darren E. (Guest)
on 2007-04-13 14:16
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> wrote:
>> Friends:
>>     :user_id,          :integer
>>    has_many :friends
>> the value equal to their user_id.
>>
>> >
>>
>
>
> --
> http://www.web-buddha.co.uk
Dave G. (Guest)
on 2007-04-13 14:22
(Received via mailing list)
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> 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.web-buddha.co.uk
Darren E. (Guest)
on 2007-04-13 14:39
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> 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.web-buddha.co.uk
This topic is locked and can not be replied to.