The situation is the following: In my app every product has attributes. for example a shirt has color, neck size and sleeve size. A jacket has color and jacket size. Those attributes have values. Colors: black, white, green, red etc Neck size: 14, 15, 16, 16 etc sleeve size: 20,21,22,23,24 etc jacket size: S, M, L, XL what I want to achieve is dynamically list all the available combination (variation) of attributes for a specific type of product. for example: a shirt can have a combination of 4 colors, 5 neck sizes and 7 sleeve sizes = 140 so I want my app to list all the available variations for shirts like black, 14, 20 black, 14, 21 black, 14, 22 ... and so on (all 140 variations in this case) so first I want to loop through sleeve size attributes, and then neck size and finally colors. what''s the best solution to achieve that? it has to be dynamic, so if the product is a jacket it has to know that it has 2 attributes (color and size) so it will loop through those like this: black, S black, M black, L black, XL white, S white, M .... and so on. has anyone encountered that kind of problem and found a good solution? Thanks for everyone. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Mark Ro wrote:> The situation is the following: > > In my app every product has attributes. for example a shirt has color, > neck size and sleeve size. > > A jacket has color and jacket size. > > Those attributes have values. > > Colors: black, white, green, red etc > Neck size: 14, 15, 16, 16 etc > sleeve size: 20,21,22,23,24 etc > jacket size: S, M, L, XLirb(main):001:0> class Array irb(main):002:1> def cross(other) irb(main):003:2> inject([]) {|a,b| other.map {|c| a << [b,c].flatten};a} irb(main):004:2> end irb(main):005:1> end => nil irb(main):006:0> [:black, :white].cross [14,15].cross %w{S M L} (irb):6: warning: parenthesize argument(s) for future version => [[:black, 14, "S"], [:black, 14, "M"], [:black, 14, "L"], [:black, 15, "S"], [:black, 15, "M"], [:black, 15, "L"], [:white, 14, "S"], [:white, 14, "M"], [:white, 14, "L"], [:white, 15, "S"], [:white, 15, "M"], [:white, 15, "L"]] irb(main):007:0> fix that parenthesize thingy and you should be good to go.. hth ilan -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Brilliant, thanks. Let''s take a real examples: colors = ["black","white","red","blue"] neck = [14,15,16,17,18] sleeve = [30,31,32,33,34] how can I change the above function (cross), so it returns a hash that includes the name of the attribute as well like: {1=>["color" =>"black", "neck" => 14, "sleeve" => 30], 2=> -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
sorry I submitted the previous post before I finished it. so I want a returned hash something like this: {1=>["color" =>"black", "neck" => 14, "sleeve" => 30], 2=>["color" =>"black", "neck" => 14, "sleeve" => 31], 3=>["color" =>"black", "neck" => 14, "sleeve" => 32], .... 100=>["color" =>"blue", "neck" => 18, "sleeve" => 34]} thanks -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---