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 => '[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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

