Mongoid each.with_index not working -


i can in plain ruby

[3,2,1].each.with_index |e, i|   p e, end  3 0 2 1 1 2 

but can't mongoid:

model.each.with_index |e, i|   p e, end 

it fails with

undefined method with_index array 

how can fix without using this:

model.each_with_index 

which not allow starting index set

in mongoid 3.1.3, with_index method works expected.

puts mongoid::version  class user   include mongoid::document   field :name, type: string end  user.create([   { name: 'harai' },   { name: 'sasaki' } ])  user.each.with_index |u, i|   puts "#{u.name}, #{i}" end 

the above code works this:

$ ruby main.rb 3.1.3 harai, 0 sasaki, 1 

your problem might because using older version of mongoid.


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

qt - Errors in generated MOC files for QT5 from cmake -