Say, you have Products for sale. Products can be in various Conditions, let''s say New, Used, and Worn. Each Product can have a different price for each different Condition that it''s in. What''s the correct way to model this in ActiveRecord? Also, if a Product goes on a LineItem, what''s a good way to say what Condition that product is? Joe
On Sep 12, 2005, at 8:56 PM, Joe Van Dyk wrote:> Say, you have Products for sale.except...> > Products can be in various Conditions, let''s say New, Used, and Worn. > Each Product can have a different price for each different Condition > that it''s in.You don''t rally sell products. You sell ProductsInAParticularCondition: your salable item is "A New Widget", not a widget that happens to be new. We have the same issue in our store: we have a book (say AWDwR), but it is sold in paper, PDF, combo pack, etc etc. We we differentiate between the product and the things we sell based on that product. Cheers Dave
On 9/12/05, Dave Thomas <dave-kbbdpT5sCmpWk0Htik3J/w@public.gmane.org> wrote:> > On Sep 12, 2005, at 8:56 PM, Joe Van Dyk wrote: > > > Say, you have Products for sale. > > except... > > > > > Products can be in various Conditions, let''s say New, Used, and Worn. > > Each Product can have a different price for each different Condition > > that it''s in. > > > You don''t rally sell products. You sell > ProductsInAParticularCondition: your salable item is "A New Widget", > not a widget that happens to be new. We have the same issue in our > store: we have a book (say AWDwR), but it is sold in paper, PDF, > combo pack, etc etc. We we differentiate between the product and the > things we sell based on that product.Err, what? I''m not following.
Joe Van Dyk wrote:> >>You don''t rally sell products. You sell >>ProductsInAParticularCondition: your salable item is "A New Widget", >>not a widget that happens to be new. We have the same issue in our >>store: we have a book (say AWDwR), but it is sold in paper, PDF, >>combo pack, etc etc. We we differentiate between the product and the >>things we sell based on that product. >> >> > >Err, what? I''m not following. > >I hope this helps ... You could have a products table (which maps to a Product model) and a conditions table (which maps to a Condition model). Then you''d have a table, which you could call items_for_sale (or products_in_a_particular_condition as Dave suggests), which has as foreign keys the ids of the products and conditions tables. Together, these foreign keys comprise the primary key of the items_for_sale table (since a product-condition combination will be unique). The items_for_sale table would have other attributes, too, such as price. Items in this table are what you''d actually be selling (e.g., "worn t-shirt"). You''d create a separate model for this (maybe called ItemForSale), which would belong_to both of the other models. Note: An attribute like description could conceivably belong to ItemForSale, as the description for a worn t-shirt might be different than the description for a brand new one. Of course, if each individual worn t-shirt has a different description (a la eBay), then you may need a more complicated system (since items for sale couldn''t uniquely be identified by product-condition pairs). Jen
On 9/12/05, jennyw <jennyw-eRDYlh02QjuxE3qeFv2dE9BPR1lH4CV8@public.gmane.org> wrote:> Joe Van Dyk wrote: > > > > >>You don''t rally sell products. You sell > >>ProductsInAParticularCondition: your salable item is "A New Widget", > >>not a widget that happens to be new. We have the same issue in our > >>store: we have a book (say AWDwR), but it is sold in paper, PDF, > >>combo pack, etc etc. We we differentiate between the product and the > >>things we sell based on that product. > >> > >> > > > >Err, what? I''m not following. > > > > > I hope this helps ... > > You could have a products table (which maps to a Product model) and a > conditions table (which maps to a Condition model). Then you''d have a > table, which you could call items_for_sale (or > products_in_a_particular_condition as Dave suggests), which has as > foreign keys the ids of the products and conditions tables. Together, > these foreign keys comprise the primary key of the items_for_sale table > (since a product-condition combination will be unique). The > items_for_sale table would have other attributes, too, such as price. > Items in this table are what you''d actually be selling (e.g., "worn > t-shirt"). You''d create a separate model for this (maybe called > ItemForSale), which would belong_to both of the other models. > > Note: An attribute like description could conceivably belong to > ItemForSale, as the description for a worn t-shirt might be different > than the description for a brand new one. Of course, if each individual > worn t-shirt has a different description (a la eBay), then you may need > a more complicated system (since items for sale couldn''t uniquely be > identified by product-condition pairs). > > JenThank you. Made much more sense. So, Products -id - description (i believe its ok to put it here) Conditions -id - name (i.e. Used, New, whatever) ProductsForSale - condition_id - product_id - quantity? - price? Where would quantity and price go? Or does each row in ProductsForSale represent one thing for sale? I''m also not sure about how stuff like pre-orders would work here. Would the ''arrival date'' be stored in the Products table?
<jennyw-eRDYlh02QjuxE3qeFv2dE9BPR1lH4CV8@public.gmane.org>
2005-Sep-14 07:25 UTC
Re: use composition? or inheritance?
Hi, Joe, Joe Van Dyk wrote:> Thank you. Made much more sense.You''re welcome!>ProductsForSale >- condition_id >- product_id >- quantity? >- price? > >Where would quantity and price go? Or does each row in >ProductsForSale represent one thing for sale?Yes, quantity on hand and price could go in the products_for_sale table/ProductForSale model.>I''m also not sure about how stuff like pre-orders would work here. >Would the ''arrival date'' be stored in the Products table?No, you can put that info in an orders table. This is because a ProductForSale can be sold to many different people, and each of those orders needs to be tracked separately. Jen
On 9/14/05, jennyw-eRDYlh02QjuxE3qeFv2dE9BPR1lH4CV8@public.gmane.org <jennyw-eRDYlh02QjuxE3qeFv2dE9BPR1lH4CV8@public.gmane.org> wrote:> > Hi, Joe, > > Joe Van Dyk wrote: > > > Thank you. Made much more sense. > > You''re welcome! > > >ProductsForSale > >- condition_id > >- product_id > >- quantity? > >- price? > > > >Where would quantity and price go? Or does each row in > >ProductsForSale represent one thing for sale? > > Yes, quantity on hand and price could go in the products_for_sale > table/ProductForSale model. > > >I''m also not sure about how stuff like pre-orders would work here. > >Would the ''arrival date'' be stored in the Products table? > > No, you can put that info in an orders table. This is because a > ProductForSale can be sold to many different people, and each of those > orders needs to be tracked separately.I don''t follow. If the store is out of the product, and the admin expects to have more of that same product in two weeks, where would that ''in two weeks'' data get stored? I don''t see how the data would belong in the orders table. The flow would be: Product in stock: sell normal Product out of stock: display "out of stock" -- allow for notifications when coming back in stock Product out of stock, but has known return date: display "coming back in <two weeks>, preorder now!"
On 9/14/05, Joe Van Dyk <joevandyk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 9/14/05, jennyw-eRDYlh02QjuxE3qeFv2dE9BPR1lH4CV8@public.gmane.org <jennyw-eRDYlh02QjuxE3qeFv2dE9BPR1lH4CV8@public.gmane.org> wrote: > > > > Hi, Joe, > > > > Joe Van Dyk wrote: > > > > > Thank you. Made much more sense. > > > > You''re welcome! > > > > >ProductsForSale > > >- condition_id > > >- product_id > > >- quantity? > > >- price? > > > > > >Where would quantity and price go? Or does each row in > > >ProductsForSale represent one thing for sale? > > > > Yes, quantity on hand and price could go in the products_for_sale > > table/ProductForSale model. > > > > >I''m also not sure about how stuff like pre-orders would work here. > > >Would the ''arrival date'' be stored in the Products table? > > > > No, you can put that info in an orders table. This is because a > > ProductForSale can be sold to many different people, and each of those > > orders needs to be tracked separately. > > I don''t follow. If the store is out of the product, and the admin > expects to have more of that same product in two weeks, where would > that ''in two weeks'' data get stored? I don''t see how the data would > belong in the orders table. > > The flow would be: > Product in stock: sell normal > Product out of stock: display "out of stock" -- allow for > notifications when coming back in stock > Product out of stock, but has known return date: display "coming back > in <two weeks>, preorder now!"Joe, I believe Jen thought you were referring to when the product would arrive to your customer, not when the product would arrive to you from your supplier. You could put the date in the ProductsForSale. I would call the field replenish_at restock_at or restocked_at.
On 9/14/05, Stephen Veit <srveit-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 9/14/05, Joe Van Dyk <joevandyk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > On 9/14/05, jennyw-eRDYlh02QjuxE3qeFv2dE9BPR1lH4CV8@public.gmane.org <jennyw-eRDYlh02QjuxE3qeFv2dE9BPR1lH4CV8@public.gmane.org> wrote: > > > > > > Hi, Joe, > > > > > > Joe Van Dyk wrote: > > > > > > > Thank you. Made much more sense. > > > > > > You''re welcome! > > > > > > >ProductsForSale > > > >- condition_id > > > >- product_id > > > >- quantity? > > > >- price? > > > > > > > >Where would quantity and price go? Or does each row in > > > >ProductsForSale represent one thing for sale? > > > > > > Yes, quantity on hand and price could go in the products_for_sale > > > table/ProductForSale model. > > > > > > >I''m also not sure about how stuff like pre-orders would work here. > > > >Would the ''arrival date'' be stored in the Products table? > > > > > > No, you can put that info in an orders table. This is because a > > > ProductForSale can be sold to many different people, and each of those > > > orders needs to be tracked separately. > > > > I don''t follow. If the store is out of the product, and the admin > > expects to have more of that same product in two weeks, where would > > that ''in two weeks'' data get stored? I don''t see how the data would > > belong in the orders table. > > > > The flow would be: > > Product in stock: sell normal > > Product out of stock: display "out of stock" -- allow for > > notifications when coming back in stock > > Product out of stock, but has known return date: display "coming back > > in <two weeks>, preorder now!" > > Joe, > > I believe Jen thought you were referring to when the product would > arrive to your customer, not when the product would arrive to you from > your supplier. You could put the date in the ProductsForSale. I would > call the field replenish_at restock_at or restocked_at.So, Products has_one ProductsForSale Conditions has_one ProductsForSale ProductsForSale belongs_to Products ProductsForSale belongs_to Conditions That look right?
On 9/19/05, Joe Van Dyk <joevandyk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 9/14/05, Stephen Veit <srveit-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > On 9/14/05, Joe Van Dyk <joevandyk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > On 9/14/05, jennyw-eRDYlh02QjuxE3qeFv2dE9BPR1lH4CV8@public.gmane.org <jennyw-eRDYlh02QjuxE3qeFv2dE9BPR1lH4CV8@public.gmane.org> wrote: > > > > > > > > Hi, Joe, > > > > > > > > Joe Van Dyk wrote: > > > > > > > > > Thank you. Made much more sense. > > > > > > > > You''re welcome! > > > > > > > > >ProductsForSale > > > > >- condition_id > > > > >- product_id > > > > >- quantity? > > > > >- price? > > > > > > > > > >Where would quantity and price go? Or does each row in > > > > >ProductsForSale represent one thing for sale?> > > > > > > > Yes, quantity on hand and price could go in the products_for_sale > > > > table/ProductForSale model. > > > > > > > > >I''m also not sure about how stuff like pre-orders would work here. > > > > >Would the ''arrival date'' be stored in the Products table? > > > > > > > > No, you can put that info in an orders table. This is because a > > > > ProductForSale can be sold to many different people, and each of those > > > > orders needs to be tracked separately. > > > > > > I don''t follow. If the store is out of the product, and the admin > > > expects to have more of that same product in two weeks, where would > > > that ''in two weeks'' data get stored? I don''t see how the data would > > > belong in the orders table. > > > > > > The flow would be: > > > Product in stock: sell normal > > > Product out of stock: display "out of stock" -- allow for > > > notifications when coming back in stock > > > Product out of stock, but has known return date: display "coming back > > > in <two weeks>, preorder now!" > > > > Joe, > > > > I believe Jen thought you were referring to when the product would > > arrive to your customer, not when the product would arrive to you from > > your supplier. You could put the date in the ProductsForSale. I would > > call the field replenish_at restock_at or restocked_at. > > > So, > Products has_one ProductsForSale > Conditions has_one ProductsForSale > ProductsForSale belongs_to Products > ProductsForSale belongs_to Conditions > > That look right?I''m trying to implement this, and my fixture looks like: settlers_of_catan_new_games_for_sale: product_id: 1 condition_id: 1 quantity: 10 price: 19.95 settlers_of_catan_used_games_for_sale: product_id: 1 condition_id: 2 quantity: 5 price: 15.95 When I try to run a unit test that loads the fixture, I get: test_truth(ProductsForSaleTest): ActiveRecord::RecordNotFound: Couldn''t find ProductsForSale without an ID Any ideas?
On 9/19/05, Joe Van Dyk <joevandyk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 9/14/05, Stephen Veit <srveit-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > On 9/14/05, Joe Van Dyk <joevandyk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > On 9/14/05, jennyw-eRDYlh02QjuxE3qeFv2dE9BPR1lH4CV8@public.gmane.org <jennyw-eRDYlh02QjuxE3qeFv2dE9BPR1lH4CV8@public.gmane.org> wrote: > > > > > > > > Hi, Joe, > > > > > > > > Joe Van Dyk wrote: > > > > > > > > > Thank you. Made much more sense. > > > > > > > > You''re welcome! > > > > > > > > >ProductsForSale > > > > >- condition_id > > > > >- product_id > > > > >- quantity? > > > > >- price? > > > > > > > > > >Where would quantity and price go? Or does each row in > > > > >ProductsForSale represent one thing for sale? > > > > > > > > Yes, quantity on hand and price could go in the products_for_sale > > > > table/ProductForSale model. > > > > > > > > >I''m also not sure about how stuff like pre-orders would work here. > > > > >Would the ''arrival date'' be stored in the Products table? > > > > > > > > No, you can put that info in an orders table. This is because a > > > > ProductForSale can be sold to many different people, and each of those > > > > orders needs to be tracked separately. > > > > > > I don''t follow. If the store is out of the product, and the admin > > > expects to have more of that same product in two weeks, where would > > > that ''in two weeks'' data get stored? I don''t see how the data would > > > belong in the orders table. > > > > > > The flow would be: > > > Product in stock: sell normal > > > Product out of stock: display "out of stock" -- allow for > > > notifications when coming back in stock > > > Product out of stock, but has known return date: display "coming back > > > in <two weeks>, preorder now!" > > > > Joe, > > > > I believe Jen thought you were referring to when the product would > > arrive to your customer, not when the product would arrive to you from > > your supplier. You could put the date in the ProductsForSale. I would > > call the field replenish_at restock_at or restocked_at. > > > So, > Products has_one ProductsForSale > Conditions has_one ProductsForSale > ProductsForSale belongs_to Products > ProductsForSale belongs_to Conditions > > That look right?I suppose it would be has_many instead of has_one, right? So, what would the model be like? What''s the right way to tell, given a certain game called "Settlers of Catan", how many new versions of that game I have, and how much those new games cost?
> > So, > > Products has_one ProductsForSale > > Conditions has_one ProductsForSale > > ProductsForSale belongs_to Products > > ProductsForSale belongs_to Conditions > > > > That look right? > > I''m trying to implement this, and my fixture looks like: > > settlers_of_catan_new_games_for_sale: > product_id: 1 > condition_id: 1 > quantity: 10 > price: 19.95 > > settlers_of_catan_used_games_for_sale: > product_id: 1 > condition_id: 2 > quantity: 5 > price: 15.95 > > When I try to run a unit test that loads the fixture, I get: > > test_truth(ProductsForSaleTest): > ActiveRecord::RecordNotFound: Couldn''t find ProductsForSale without an ID > > Any ideas?Ok, restating my problem: I have Products, Conditions, and ProductsForSale. ProductsForSale has a product_id, a condition_id, a price, and a quantity. ProductsForSale belongs_to Products and Conditions. Products and Conditions both has_many ProductsForSale. My table is called products_for_sale and I have ''set_table_name :products_for_sale'' in the model for ProductsForSale. I have a test fixture called products_for_sale.yml. I''m trying to load that fixture in products_test.rb. So, I have "fixtures :products, :conditions, :products_for_sale" in that test file. I get a ''ActiveRecord::RecordNotFound: Couldn''t find ProductsForSale without an ID'' error when I run that unit test.
On 9/19/05, Joe Van Dyk <joevandyk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > So, > > > Products has_one ProductsForSale > > > Conditions has_one ProductsForSale > > > ProductsForSale belongs_to Products > > > ProductsForSale belongs_to Conditions > > > > > > That look right? > > > > I''m trying to implement this, and my fixture looks like: > > > > settlers_of_catan_new_games_for_sale: > > product_id: 1 > > condition_id: 1 > > quantity: 10 > > price: 19.95 > > > > settlers_of_catan_used_games_for_sale: > > product_id: 1 > > condition_id: 2 > > quantity: 5 > > price: 15.95 > > > > When I try to run a unit test that loads the fixture, I get: > > > > test_truth(ProductsForSaleTest): > > ActiveRecord::RecordNotFound: Couldn''t find ProductsForSale without an ID > > > > Any ideas? > > Ok, restating my problem: I have Products, Conditions, and > ProductsForSale. ProductsForSale has a product_id, a condition_id, a > price, and a quantity. ProductsForSale belongs_to Products and > Conditions. Products and Conditions both has_many ProductsForSale. > My table is called products_for_sale and I have ''set_table_name > :products_for_sale'' in the model for ProductsForSale. > > I have a test fixture called products_for_sale.yml. I''m trying to > load that fixture in products_test.rb. So, I have "fixtures > :products, :conditions, :products_for_sale" in that test file. > > I get a ''ActiveRecord::RecordNotFound: Couldn''t find ProductsForSale > without an ID'' error when I run that unit test.Giving ProductsForSale an id field cleared up the issue.
I''m guessing, but I''m assuming you''ll need an id column for ProductForSale and you''ll also need to correctly populate the products and conditions tables ? A. A. On 20 Sep 2005, at 01:07, rails-request-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org wrote:>> settlers_of_catan_used_games_for_sale: >> product_id: 1 >> condition_id: 2 >> quantity: 5 >> price: 15.95 >-- mailto:alancfrancis-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org http://blog.alancfrancis.com/