Hi I'm very new to rails and need some help on iterating through nested arrays. I'm trying to figure out how to do mass-inserts for each values that is different between the make, model and color of cars.
The params that are pass are:
"make" => ["Honda", "Honda"],
"model" => ["Civic", "Accord"],
"color" => [{"Black", "White", "Red"}, {"Black", "White"}]
So with these params passed, I wanted to have 5 inserts to occur.
1. Honda - Civic - Black 2. Honda - Civic - White 3. Honda - Civic - Red 4. Honda - Accord - Black 5. Honda - Accord - White
Here what I've got so far that pushes the insert query that builds it. But I'm unsure how to make it insert 5 times according to what I've listed above:
def self.cars(make, model, color)
inserts = []
color.each do |i|
inserts.push "('#{make}', '#{model}', '#{i}')"
end
Foo.connection.execute "INSERT INTO car_inventory (make, model, color) VALUES #{inserts.join(", ")}"
end