spencer
spencer

Reputation: 41

Setting up product options to generate part numbers?

I am trying to build a nice robust system in Rails to create products. At the top level I have Products, which are pretty generic (for example, T-Shirt). Each product can have unlimited Option Types (Size, Color), which in turn can have unlimited Option Values (Small, Medium, Large; Red, Blue, Green).

I set up my models so that Products have many Option Types, and Option Types have many Option Values.

Where I'm stuck is how to build Part Numbers. Part Numbers are individual values that are created based on the selected Option Types/Values for a given Product. For example, TSHIRT-LG might be the part number for a T-Shirt with Size: Large, and Color: Green selected.

I guess my strategy so far has been to associate Part Numbers with Products and Option Values. In the back end, an administrator should create Part Numbers by selecting combinations of Option Values belonging to Option Types belonging to a Product. On the front end, a user should be able to select their preferred options, and then see the correct Part Number appear based on their selections. I've created a Product form using nested models, based on http://railscasts.com/episodes/196-nested-model-form-revised. Creating Options hasn't been a problem, but I'm stuck on how to build the form to create Part Numbers based on Option Values. I'd like to do drop-down menus to choose existing Option Values, but I just can't get my head around making it work.

Upvotes: 0

Views: 316

Answers (2)

maxcobmara
maxcobmara

Reputation: 391

First you need to decide how exactly you want to code your part-numbers and what attributes you want to take to create the part numbers and what you want them to look like.

e.g TSHIRT-XLG-M-P-001

Have a fields called part_no, and as the admin creates items

before_save: create_part_no
validates_uniqueness_of :part_no

def create_part_no
  name + '-' + size + '-' + gender+ '-' + id
end

if you want it changeable by the admin remove the before_save and

<%= f.text_field :part_no, default => :create_part_no %>

some ajax may be required

Upvotes: 2

Vapire
Vapire

Reputation: 4588

Ryan also has an older but great screen cast on dynamic select menus: http://railscasts.com/episodes/88-dynamic-select-menus

Maybe it helps!

Upvotes: 1

Related Questions