I have this loop:
car_data = Hash.new
Car.all.each do |c|
car_data[c.brand] = c.id
car_data['NEW'] << c.id if c.new == 1
end
I have this snipper and trying to save all the new cars to car_data['NEW'], but this code keeps only one item in the hash (there should be 8).
I also tried to define that car_data['NEW'] as an array:
car_data = Hash.new
car_data['NEW'] = Hash.new
Car.all.each do |c|
car_data[c.brand] = c.id
car_data['NEW'] << c.id if c.new == 1
end
But the result was the same - just one item. How do I save the whole array to the hash key element?
Thank you.
{ }for a new Hash, that callingHash.newis only necessary when supplying defaults likeHash.new(0). Less is more.