Rails - CSV(导出到CSV)循环 [英] Rails - CSV(export to CSV) loop

查看:138
本文介绍了Rails - CSV(导出到CSV)循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要(以CSV格式导出)以前50个学生和接下来的50个学生等的形式,在单独的文件中。
我试过下面给出的代码,我不知道如何生成循环,请提供一些代码来做的过程。

i want to do (export to CSV) in the form of fetching first 50 students and next 50 students and so on, in seperate files. i have tried with the below given code, and i dont know how to generate loop, please provide some code to do the process.

 @count =0
 if @count == 0
 students = Student.find(:all,:order => 'name', :limit => 50)
 @count = @count+1
 else
 students = Student.find(:all,:order => 'name', :limit => 50, :offset => 50)
 @count = @count+1

在点击导出到csv时,它应该获取前50名学生,后50名学生等等在不同的文件。

on clicking export to csv, it should fetch 1st 50 students, next 50 students and so on in different files.

请告诉如何在上面的代码中添加循环,以便每隔50个学生在单独的文件中获取

please, tell how to add loop to above code so that it fetches every 50 students, in seperate files

推荐答案

响应于IV,并建立在他的答案你可以做

in response to theIV and building on his answer you could do

Student.all(:order => 'name').in_groups_of(50, false).each_with_index do |group,index|
  export_to_csv("file_number#{index}.csv", group)
end

这篇关于Rails - CSV(导出到CSV)循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆