Forum: Ruby on Rails linked table confusion

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.
Craig W. (Guest)
on 2006-02-06 23:29
(Received via mailing list)
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
Estelle W. (Guest)
on 2006-02-07 01:29
(Received via mailing list)
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.
Estelle W. (Guest)
on 2006-02-07 01:35
(Received via mailing list)
Craig,

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

Estelle.
Craig W. (Guest)
on 2006-02-07 02:21
(Received via mailing list)
Estelle (my grandmother's name), that was brilliant - it solved my
issues.

Thanks

Craig

and thanks for the link to the mini cheatsheet.
This topic is locked and can not be replied to.