Forum: Ruby on Rails trouble wtih webstore schema, handling product variations

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Peter Peter Pumpkin Eater (Guest)
on 2006-06-07 09:50
Hi Railers,

Does anyone have any guidance on setting up a simple way to handle
product variations (e.g garment size/color)? Initially, I had thought
that I would make a variation model which defined the extra bits, but it
seems quite inflexible especially so if a store were to sell
non-clothing items that also had variants of one flavor or another.

product
  has_many variations
  title
  description
  price
  variations

variation
  belongs_to garment
  has_many images
  size
  color
  sku

would it make more sense for the variation model to subclass the product
and add additional table columns?


As an aside, is anyone else having problems with the list and their
GMail account? I have not received a message since 8h PST.
Chris T (Guest)
on 2006-06-07 18:00
(Received via mailing list)
I used STI for something slightly similar for something where each item
was a type of model, but it had additional characteristics depending on
whether it was a mainmodel or modelvariation (actually had three levels
-- parentmodel, mainmodel, and modelvariation).

class Modeltype < ActiveRecord::Base
..
end
class Modelvar < Modeltype
..
end

I've then got a one-to-many self-referential relationship on the model
to define the relationship.

However, re-reading your post, wondering if in your case you have a
many-many relationship between products and possible product variations:
a product can have many variations, and presumably some of those
variations -- e.g. extra large -- can be associated with other products.

So what about
variation
    HABTM products
    variation_group (e.g. size, colour)
    variation_specs (e.g. red, small)

I guess you could even make variation an STI if the variations required
different details.

IF you used has_many through the join table could contain the SKU.

Then again I could be talking rot -- had a fruitless morning trying to
sort out a mixin prob...

p.s. Yup, I've had the same prob with gmail. Seems OK now though.
Sean O'Hara (Guest)
on 2006-06-07 19:14
(Received via mailing list)
Hi Peter,

I am pretty much a newb, but I had basically the same problem. In my
schema I have Products and 3 sub products that inherit from Products
using STI. And I have an Options class (similar to your Variations)
that belongs_to one of the sub product classes (because the other two
will never need options). What I did is create to different methods
for adding items to a cart. The first is add_to_cart, the second is
add_option_to_cart. And an option contains a Product object or
reference. Basically I did it this way because I just didn't think
that Options had enough in common with Products for them to inherit
from Product.

I would however be interested in knowing if there's a simpler way of
doing this that doesn't require using two different functions to add
to the cart (and subsequently dealing with the items in the cart when
it's time to submit an order).

Here's my models...

class Product < ActiveRecord::Base
   has_many :line_items

     t.column "price_ca", :integer
     t.column "price_us", :integer
     t.column "catalog_number", :string
     t.column "type", :string
     t.column "form", :string
     t.column "order_number", :integer
     t.column "title", :string
     t.column "has_option", :boolean, :default => false, :null => false
     t.column "quantity", :integer
     t.column "created_at", :datetime
     t.column "updated_at", :datetime
end

class HardProduct < Product
   has_many :options
end

class Option < ActiveRecord::Base
   belongs_to :hard_product
   has_many :line_items

     t.column "hard_product_id", :integer
     t.column "description", :string
     t.column "quantity", :integer
end

Take care,
Sean
Peter Peter Pumpkin Eater (Guest)
on 2006-06-08 08:29
Thank you both, you have given me some good ideas on how to tackle this
=)

Sean O'Hara wrote:
> Hi Peter,
>
> I am pretty much a newb, but I had basically the same problem. In my
> schema I have Products and 3 sub products that inherit from Products
> using STI. And I have an Options class (similar to your Variations)
> that belongs_to one of the sub product classes (because the other two
> will never need options). What I did is create to different methods
> for adding items to a cart. The first is add_to_cart, the second is
> add_option_to_cart. And an option contains a Product object or
> reference. Basically I did it this way because I just didn't think
> that Options had enough in common with Products for them to inherit
> from Product.
This topic is locked and can not be replied to.