Reputation: 909
I have an object, videos. I wanted to add id of video with its duration on a .each loop in Rails. Something like the following --
videos = {}
Products.each do |p, index|
videos << p.id => p.duration
end
To get a sample set result like this --
videos = { 1: 2345, 2: 3456, 3: 4567 }
Upvotes: 1
Views: 3328
Reputation: 6749
You should be using each_with_index
:
videos = Hash.new
Products.each_with_index do |p, index|
videos.merge(p.id => p.duration)
end
Upvotes: 2