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 -- Posted via http://www.ruby-forum.com/.
Ken Bowley
2005-Dec-07 05:20 UTC
Re: Tables with two has_and_belongs_to_many releationships
On Wed, 7 Dec 2005, Ed Schechter <schechtere-PkbjNfxxIARBDgjK7y7TUQ@public.gmane.org> wrote:> 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?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 %>