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 > endproduct.rb> class Product < ActiveRecord::Base > has_and_belongs_to_many :orders > endorder.rb> class Order < ActiveRecord::Base > belongs_to :customer > has_and_belongs_to_many :products > endDB 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 > endDB 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 > endDB 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 > endDB 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 > endseeds.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-ElnL8FsqGlyJIAd+swBSYQ@public.gmane.org'', > :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 joe-ElnL8FsqGlyJIAd+swBSYQ@public.gmane.org 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
I whonder why a product should have many orders? It belongs to orders..> > class Customer < ActiveRecord::Base > > has_many :orders > > end > > class Order < ActiveRecord::Base > > belongs_to :customer > > has_many :products > > end > > class Product < ActiveRecord::Base > > belongs_to :orders > > end-- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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> >>> class Customer < ActiveRecord::Base >>> has_many :orders >>> end >>> class Order < ActiveRecord::Base >>> belongs_to :customer >>> has_many :products >>> end >>> class Product < ActiveRecord::Base >>> belongs_to :orders >>> end > > -- > 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. >-- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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... On Tue, Jul 26, 2011 at 10:01 PM, Peter Bell <peter-AS9tOuWgs4cAvxtiuMwx3w@public.gmane.org> wrote:> 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 > > > > >>> class Customer < ActiveRecord::Base > >>> has_many :orders > >>> end > >>> class Order < ActiveRecord::Base > >>> belongs_to :customer > >>> has_many :products > >>> end > >>> class Product < ActiveRecord::Base > >>> belongs_to :orders > >>> end > > > > -- > > 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > > > > -- > 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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! On Jul 26, 5:14 pm, Chris Kottom <ch...-kMviOf/NVQxZWXO/OqhO/A@public.gmane.org> wrote:> 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... > > > > > > > > On Tue, Jul 26, 2011 at 10:01 PM, Peter Bell <pe...-AS9tOuWgs4cAvxtiuMwx3w@public.gmane.org> wrote: > > 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 > > > >>> class Customer < ActiveRecord::Base > > >>> has_many :orders > > >>> end > > >>> class Order < ActiveRecord::Base > > >>> belongs_to :customer > > >>> has_many :products > > >>> end > > >>> class Product < ActiveRecord::Base > > >>> belongs_to :orders > > >>> end > > > > -- > > > 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 rubyonrails-talk-/JYPxA39Uh4Ykp1iOSErHA@public.gmane.orgm. > > > To unsubscribe from this group, send email to > > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > > For more options, visit this group at > >http://groups.google.com/group/rubyonrails-talk?hl=en. > > > -- > > 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > To unsubscribe from this group, send email to > > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > For more options, visit this group at > >http://groups.google.com/group/rubyonrails-talk?hl=en.-- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.