我必须重新排序我的目的地散列,所以我想在这样的数组中创建一个数组:
@orderedDestinations = Array.new
@destinations.each do |destination|
if (destination.position != nil)
@orderedDestinations[destination.position][destination.id] = destination
end
end
我遇到了这个错误:
You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.[]=
我做错了什么?
最佳答案
如果你想按 Destination#position
对 @destinations
进行排序,你应该这样做:
@orderedDestinations = @destinations.sort_by(&:position)
http://ruby-doc.org/core-1.9.2/Enumerable.html#method-i-sort_by
完成交易。
关于ruby-on-rails - 带 ruby on rails 的多维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7809785/