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

class CreateProducts < ActiveRecord::Migration

def self.up

create_table :products do |t|

t.string :title

t.string :image_url

t.decimal :price, :precision => 8, :scale => 2

t.timestamps

end

end

def self.down

drop_table :products

end

end

DB Migration - Customers

class CreateCustomers < ActiveRecord::Migration

def self.up

create_table :customers do |t|

t.string :first_name

t.string :last_name

t.string :email

t.string :twitter

t.timestamps

end

end

def self.down

drop_table :customers

end

end

DB Migration - Orders

class CreateOrders < ActiveRecord::Migration

def self.up

create_table :orders do |t|

t.timestamp :date

t.decimal :total

t.integer :customer_id

t.timestamps

end

end

def self.down

drop_table :orders

end

end

DB Migration - Orders & Products Join

class CreateOrderProductJoinTable < ActiveRecord::Migration

def self.up

create_table :orders_products, :id => false do |t|

t.integer :product_id

t.integer :order_id

end

end

def self.down

drop_table :orders_products

end

end

seeds.rb

#-------------------

CREATE PRODUCTS

#-------------------

Product.delete_all

Product.create(

:title => ‘Liftoff’,

:image_url => 'http://joeworkman.net/depot/products/liftoff/liftoff-78.png’,

:price => 9.95)

Product.create(

:title => ‘Expose’,

:image_url => 'http://joeworkman.net/depot/products/expose/expose-78.png’,

:price => 14.95)

Product.create(

:title => ‘Comments’,

:image_url => 'http://joeworkman.net/depot/products/comments/comments-78.png’,

:price => 4.95)

#-------------------

CREATE CUSTOMERS

#-------------------

Customer.delete_all

Customer.create(

:first_name => ‘Joe’,

:last_name => ‘Workman’,

:email => ‘joe@workmanmail.com’,

: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?

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

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!