Tables with two has_and_belongs_to_many releationships


#1

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

<%= h @order.origin_location.address1 %> <%= h @order.origin_location.city %> ...

When I invoke order#edit, I get

undefined method `address1’ for Location:Class


Can anyone illuminate me where I have gone astray?

Thanks
Ed


#2

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| %>

<%= h location.address1 %> <%= h location.city %> ... <% end %>