Rails ActiveRecord 有无按数量切割结果的内置方法?

nine · July 11, 2015 · Last by nine replied at July 11, 2015 · 1840 hits

达到如下效果

pool  = []
ModelName.all.each_with_index do |obj , index|
  pool.push [] if index % num == 0
  pool.last.push obj
end
pool
(1..10).each_slice(3).to_a 
# => [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10]]
You need to Sign in before reply, if you don't have an account, please Sign up first.