Linked table confusion


#1

placement.rb
has_one :client
has_one :case_manager

client.rb
belongs_to :case_manager
has_many :placements

case_manager.rb
has_many :clients
has_many :placements

I am trying to create a view file for placements.

I can pull columns from clients table in this view by using…

<%= @placement.client.wholename %>

but if I use

<% @placement.case_manager.wholename %>

I get error…

RuntimeError: ERROR C42703 Mcolumn case_managers.placement_id does
not exist Fparse_func.c L1359 Runknown_attribute: SELECT * FROM
case_managers WHERE (case_managers.placement_id = 4) LIMIT 1

which makes sense in that I don’t have a ‘placement_id’ column in
case_managers table. But case manager has many placments so I did create
a join table - case_managers_placements in which there are 2 fields…
placement_id
case_manager_id
and foreign keys for both fields to their respective tables
then I inserted 1 record, for the placement with the proper placement_id
and case_manager_id for the join…

I still get the above error.

How does one work through this situation?

Craig


#2

Craig,

I think you have confused the relationship between placements and
case_managers. If its a one-to-many then you shouldn’t be using a
join table. Just put your foreign table references, case_manager_id
and client_id, into the placements table.

Then your placement model would be:

placement.rb
belongs_to :client
belongs_to :case_manager

Estelle.


#3

Craig,

You might find this tutorial handy…
http://www.slash7.com/cheats/activerecord_cheatsheet.pdf

Estelle.


#4

Estelle (my grandmother’s name), that was brilliant - it solved my
issues.

Thanks

Craig

and thanks for the link to the mini cheatsheet.