Forum: Ruby on Rails Tables with two has_and_belongs_to_many releationships

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.
Ed Schechter <> (Guest)
on 2005-12-07 06:59
Newbie issue, probably:

---------------------------
Background:
Have two tables:

Order
  id

Location
  id
  address1
  city
  state
  (etc.)

Each order has one or more origin and  destination locations.  (The
order is an order for a transportation company to ship something

Each location can exist on multiple orders.

Have the following link tables:

order_origin_location_link
  order_id
  location_id

order_destination_location_link
  order_id
  location_id

-------------------------------------

Attempted solution

Order.rb:

class Order < ActiveRecord::Base
	belongs_to :vendor
	belongs_to :customer

	has_and_belongs_to_many :origin_location,
			:class_name => 'Location',
			:join_table => 'order_origin_location_link',
			:foreign_key => 'order_id',
			:association_foreign_key => 'location_id'

	has_and_belongs_to_many :destination_location,
			:class_name => 'Location',
			:join_table => 'order_destination_location_link',
			:foreign_key => 'order_id',
			:association_foreign_key => 'location_id'


	def add_stop

	end

end

-----------------

order#edit.rhtml

...
<tr><td><%=  h @order.origin_location.address1  %></td></tr>
<tr><td><%=  h @order.origin_location.city  %></td>
...

----------------

When I invoke order#edit, I get

undefined method `address1' for Location:Class

---------------

Can anyone illuminate me where I have gone astray?

Thanks
Ed
rails (Guest)
on 2005-12-07 07:22
(Received via mailing list)
On Wed, 7 Dec 2005, Ed Schechter <removed_email_address@domain.invalid> wrote:

>
> undefined method `address1' for Location:Class
>
> ---------------
>
> Can anyone illuminate me where I have gone astray?

You can have more than one origin_location, so you will need to iterate
over @order.origin_locations such as:

<% @order.origin_locations.each do |location| %>
...
<tr><td><%=  h location.address1  %></td></tr>
<tr><td><%=  h location.city  %></td>
...
<% end %>
This topic is locked and can not be replied to.