Chris, you lucky guy. Relationships with multiple models? I cannot help you,
since I''ve never had a relationship with even one model, unless you
count
someone that showed up once modeling a gas grill in the Sears catalog.
On 10/21/05, Chris Hall
<christopher.k.hall-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:>
> given the following:
>
> orders
> ----------
> id
> order_num
> ...
>
> line_items
> ----------
> id
> order_id
> quantity
> .....
>
> boxes
> ----------
> id
> code
> prod_num
> .....
>
> boxes_line_items
> ----------
> box_id
> line_item_id
>
>
> class Order < ActiveRecord::Base
> has_many :line_items
> end
>
> class LineItem < ActiveRecord::Base
> belongs_to :order
> has_and_belongs_to_many :boxes
> end
>
> class Box < ActiveRecord::Base
> has_and_belongs_to_many :line_items
> end
>
>
> now multiple LineItems can refer to the same box because the box
> itself is really just a container for a bunch of parts. the
> LineItem.quantity refers to how many parts need to be removed from the
> box for the order. Therefore order 1 and order 2 can both have line
> items that refer to box ABC.
>
> so i have a maintenance script that is reading from a legacy system
> using DBI:ODBC. each row in the legacy system looks something like
> this. i''ve cut out the extraneous information to make explaination
a
> bit easier.
>
> legacy row = ordernum, quantity, boxcode, prod_num (ex,
''123456'', 500,
> ''123XYZ'', 123)
>
> first i create an order instance and box instance:
>
> # row is a hash read in from a legacy
>
> transaction do
> order = Order.find_by_order_num(row["ORDERNUM"])
> order = Order.new(:order_num => row["ORDERNUM"]) if order.nil?
>
> box = Box.find_by_code(row["BOXCODE"])
> box = box.new(:code => row["BOXCODE"], :prod_num =>
item["PRODNUM"])
> if box.nil?
>
> # create line item and save
> ...
> end
>
> then i need to create the line item itself. this is where i am having
> a bit of trouble understanding how to link the order and box to the
> line item, based on the relationships
>
> i''m thinking:
>
> li = LIneItem.new()
> li.quantity = row["QUANTITY"].to_i
> li.order = order
> li.box = box
> li.save
>
> is it that simple? will this write ALL the data, including the data
> to the boxes_line_items join table for me and save the order and box
> if they are new ones (didn''t already exist in the database)?
>
> Chris
> _______________________________________________
> Rails mailing list
> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>
>
--
Dennis Sutch
dennis-+mObuC4wx48AvxtiuMwx3w@public.gmane.org
_______________________________________________
Rails mailing list
Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
http://lists.rubyonrails.org/mailman/listinfo/rails