Mongoid each.with_索引不起作用
我可以用纯红宝石做这个Mongoid each.with_索引不起作用,mongoid,Mongoid,我可以用纯红宝石做这个 [3,2,1].each.with_index do |e, i| p e, i end 3 0 2 1 1 2 但我不能对Mongoid这样做: Model.each.with_index do |e, i| p e, i end 它失败了 undefined method with_index for Array 如果不使用此选项,如何修复此问题: Model.each_with_index 这不允许在Mongoid 3.1.3中设置起始索引,和_i
[3,2,1].each.with_index do |e, i|
p e, i
end
3
0
2
1
1
2
但我不能对Mongoid这样做:
Model.each.with_index do |e, i|
p e, i
end
它失败了
undefined method with_index for Array
如果不使用此选项,如何修复此问题:
Model.each_with_index
这不允许在Mongoid 3.1.3中设置起始索引,
和_index
方法按预期工作
puts Mongoid::VERSION
class User
include Mongoid::Document
field :name, type: String
end
User.create([
{ name: 'Harai' },
{ name: 'Sasaki' }
])
User.each.with_index do |u, i|
puts "#{u.name}, #{i}"
end
上述代码的工作原理如下:
$ ruby main.rb
3.1.3
Harai, 0
Sasaki, 1
您的问题可能是因为您使用的是旧版本的Mongoid