Creating a Simple Orders System

I am trying to create a very simple order & customer tracking system
with Customer, Products and Orders. I am having challenges getting my
head around how to code to setup the many-to-many relationship between
Orders and Products. Here are some code snippets to show how things are
setup.

customer.rb

class Customer < ActiveRecord::Base
has_many :orders
end

product.rb

class Product < ActiveRecord::Base
has_and_belongs_to_many :orders
end

order.rb

class Order < ActiveRecord::Base
belongs_to :customer
has_and_belongs_to_many :products
end

DB Migration - Products

def self.down
drop_table :products
end
end

DB Migration - Customers

def self.down
drop_table :customers
end
end

DB Migration - Orders

def self.down
drop_table :orders
end
end

DB Migration - Orders & Products Join

end
end

seeds.rb

:image_url => 

http://joeworkman.net/depot/products/expose/expose-78.png’,

:first_name => ‘Joe’,
:last_name => ‘Workman’,
:email => ‘[email protected]’,
:twitter => ‘joeworkman’)
#-------------------

CREATE ORDERS

#-------------------
Order.delete_all
Order.create(
:date => Time.now,
:customer_id => 1)

So my questions are….

Is this setup looking correct?
How can I seed orders with multiple products?
What would my New order creation method look like? Similar to the seed
function?


Cheers,
Joe

Joe Workman Developing cool things for the Mac…
joeworkman joeworkman.net [email protected] About.me
Get my vCardSent from my 

I whonder why a product should have many orders? It belongs to
orders…

On Jul 26, 2011, at 3:58 PM, KlausG wrote:

I whonder why a product should have many orders? It belongs to
orders…

Not being paying much attention to this thread, but whenever I see
comments like this I’m cautious. Bear in mind that if products are
mutable (can vary over time), typically you want to replicate some of
their data as of a point in time so that a order has-many order_items
which contain product information as at that point in time.

Otherwise if you’re not careful, updating the price on your products
completely changes the value of all of your historic orders containing
that product.

Apologies if it’s not relevant to the thread, but if it is, it’s
something that should be considered in the design.

Best Wishes,
Peter

The term you’re looking for is LineItem, and it is typical to include
things
like Peter suggests above – snapshot information of any Product info
that
would be necessary for printing invoices – as well as tax calculations,
fulfillment status, return status, etc. In this case, you would
probably
want the following relationships:

Product has many LineItems / LineItem belongs to Product
Order has many LineItems / LineItem belongs to Order
Order has many Products through LineItems
Product has many Orders through LineItems

HTH…

I took your advice and added a LineItem Model into the mix as well.
Thank you very much I will post back later with how things are going.
I really appreciate the help!