0

I have a Rails 4 app using PostgreSQL. My app provides a lists of desks. Each desk can come in 1 or more colors (blue,red,green, etc...)

Given a model Desk, what is the best way to store the available color per desk?

I could do something like Desk.color_options and just have a list but then if I want to show all desks that come in the color red I wouldn't be able to sort through the desk records efficiently.

halfer
  • 19,824
  • 17
  • 99
  • 186
AnApprentice
  • 108,152
  • 195
  • 629
  • 1,012
  • What you're referring to is typically referred to as “variants” in the ecomm and manufacturing industries. – coreyward Jan 30 '17 at 16:54
  • 1
    [This is a great reference for such questions.](http://stackoverflow.com/questions/4048151/what-are-the-options-for-storing-hierarchical-data-in-a-relational-database) – coreyward Jan 30 '17 at 16:55
  • Thanks, which option do you recommend? – AnApprentice Jan 30 '17 at 16:56
  • To clarify, I appreciate the reference. The challenge is I'm aware of most of the options, I'm just not sure which is the right one to move forward with. I'm hoping someone with more experience can make a recommendation from the list so I don't head down the wrong path. Ideas? Thanks! – AnApprentice Jan 30 '17 at 17:10

1 Answers1

3

I might do something like this to start:

class Product < ApplicationRecord
  has_many :product_options
  scope :with_variant, -> (variant) {
    joins(:product_options).merge(ProductOption.by_variant(variant))
  }
end

class ProductOption < ApplicationRecord
  belongs_to :product
  belongs_to :variant
  validates :stock_level, numericality: 0..1000

  scope :by_variant, -> (variant) { where(variant: variant) }
end

class Variant < ApplicationRecord
  scope :by_attr, -> (attr_name, attr_value) { 
    where(attribute: attr_name, value: attr_value)
  }
end

If you want to find all desks:

Product.all

If you only want red desks:

Product.by_variant(Variant.by_attr('color', 'red'))
coreyward
  • 77,547
  • 20
  • 137
  • 166
  • thanks for the answer.... How would the variant red get into the database? Would I seed the Product Options? – AnApprentice Jan 31 '17 at 02:43
  • Also, what is `ApplicationRecord` are these three all models? Would Variant be a table in the db? – AnApprentice Jan 31 '17 at 02:44
  • Also, is this Rails 4 friendly? – AnApprentice Jan 31 '17 at 02:45
  • 1
    @AnApprentice 1) You probably want an admin interface of some kind for defining that information, but you could use Seed data to prepare a dev environment. 2) `ApplicationRecord` is a Rails 5 thing, but you can use it in 4 as well. In this case, it's safe to replace it with `ActiveRecord::Base`, but for the sake of letting this answer work for the future I'll leave the newer construct. 3) Everything else here should work fine in Rails 4. – coreyward Jan 31 '17 at 03:08
  • 1
    @AnApprentice Oh, and re: tables/models: I gave the ActiveRecord models. The tables would reflect this, and should be pretty easy to back into from here, but if you end up running into an error while doing that you can always ask another question. – coreyward Jan 31 '17 at 03:11