# Table look-up method advise please

**URL:** <https://www.ruby-forum.com/t/table-look-up-method-advise-please/134992>\
**Category:** Rails\
**Created:** [April 16, 2008, 9:47pm UTC](https://www.ruby-forum.com/t/table-look-up-method-advise-please/134992 "2008-04-16T21:47:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![technomancy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b77776/32.png) [@technomancy](https://www.ruby-forum.com/u/technomancy)\
**Post date:** [April 16, 2008, 9:47pm UTC](https://www.ruby-forum.com/t/table-look-up-method-advise-please/134992/1 "2008-04-16T21:47:35Z")

</div>

Hi  
I am trying to learn rails 2 but really struggling with all this table  
relationship stuff & restful routes with rails 1 manuals etc!

I was wondering if somebody would be kind enough to give me some advice  
and sample code for my very simple requirement.

I have two tables…

1. Incoming telephone calls table with telephone number field
2. Customer table with telephone field

my requirement is for the list of incoming numbers to be shown in the  
index.html page -easy no problem I can do this! 🙂

but I am struggling with the best method here…

when a ‘show’ link is clicked on a row then the show.html page will show  
the incoming call details but also with a partial (i guess) show the  
customers details automatically.

Do I join the tables or do I execute a find?

please help and thanks for you time.

---

<div class="post-metadata">

**Author:** ![technomancy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b77776/32.png) [@technomancy](https://www.ruby-forum.com/u/technomancy)\
**Post date:** [April 16, 2008, 11:08pm UTC](https://www.ruby-forum.com/t/table-look-up-method-advise-please/134992/2 "2008-04-16T23:08:58Z")

</div>

On Apr 16, 3:47 pm, Phil H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> 1. Customer table with telephone field  
> Do I join the tables or do I execute a find?
> 
> please help and thanks for you time.

You can setup your call entry to :has\_one :user, and specify the  
column names that should match. Then you can do entry.user to grab  
the user details.

This is much easier if you do things the way Rails expects (I am  
mixing schema and model statements here, I think you know which is  
which):

## Calls

## id telephone\_number\_id

:belongs\_to :telephone\_number

## TelephoneNumbers

## id number

:has\_many :calls  
:has\_one :user

## Users

## id telephone\_number\_id

:belongs\_to: telephone\_number

---

<div class="post-metadata">

**Author:** ![technomancy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b77776/32.png) [@technomancy](https://www.ruby-forum.com/u/technomancy)\
**Post date:** [April 19, 2008, 9:43pm UTC](https://www.ruby-forum.com/t/table-look-up-method-advise-please/134992/3 "2008-04-19T21:43:43Z")

</div>

> You can setup your call entry to :has\_one :user, and specify the  
> column names that should match. Then you can do entry.user to grab  
> the user details.
> 
> This is much easier if you do things the way Rails expects (I am  
> mixing schema and model statements here, I think you know which is  
> which):
> 
> ## Calls
> 
> ## id telephone\_number\_id
> 
> :belongs\_to :telephone\_number
> 
> ## TelephoneNumbers
> 
> ## id number
> 
> :has\_many :calls  
> :has\_one :user
> 
> ## Users
> 
> ## id telephone\_number\_id
> 
> :belongs\_to: telephone\_number

Thanks for helping Clever.

I must say I am still struggling to get this to work correctly.

My biggest problem with the teaching books and the very good screencasts  
available on the web is that they all show how to set up these fantastic  
‘has\_and\_belong\_to\_methods\_etc.’ but they then stop and don’t show the  
code you need to put in your controllers or views, and if they do show a  
glimpse of code it’s for an outlandish over the top multi-complex  
shopping cart! - leaving us poor total newbies banging our heads on our  
keyboards after hours of failed code attempts. (sorry I feel much better  
now I have shared my frustration)

so sorry to say - I still have more questions to ask!

In my tables I have the telephone\_numbers\_id fields however they are set  
up as VARCAR(20) and not integers - will this still work?

Do I need to create a join table or a join model? I think in this case  
it needs to be a join model as the documentation would appear to  
indicate a join table is for ‘many and through’ types of associations  
-am i correct in this assumption?

I have also tried to create this as a ‘find method’ to look up the  
user\_telephone number but it keeps going wrong because it is looking up  
the calls table ‘id’ and not the call table ‘telephone\_number\_id’ - i  
don’t want to spend any more time on this method because it would appear  
that i am doing it the hard or wrong way.

So if you are able to show me some code for my controllers & views or  
point me to a good book or online tutorial then I would be very, very  
grateful.
