I have an array with Product Ids
and Color HEX Codes
Input:
@campaign.selectedproducts
Output:
["2,333333","1,333333",4,444444"]
I'm trying to make a new array with all of the product data by finding it with id:
@selectedgifts = @campaign.selectedproducts.collect [{|i| Product.find(i) }, |i| i.split(',').last]
Array should output
["Product Object, HEX code", "Product Object, HEX code"]
¿Any help?
Thanks!
CodePudding user response:
You can try with group_by method like below:
@campaign.selectedproducts.group_by(&:color_code).transform_values{|val| val.pluck(:id).uniq}
CodePudding user response:
Converting Product object to string is not what you want i guess. So i suggest you storing each element as hash instead of string.
@campaign.selectedproducts.map do |string|
id, hex_code = string.split(',')
product = Product.find(id)
{ product => hex_code }
end
Edit: Or you can store each element as array like this:
@campaign.selectedproducts.map do |string|
id, hex_code = string.split(',')
product = Product.find(id)
[product, hex_code]
end