Table look-up method advise please

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! :slight_smile:

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.

On Apr 16, 3:47 pm, Phil H. [email protected]
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

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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs