CSV parsing & writing from a migration file

Well it’s not a good idea to put a CSV export in a ruby migration file. But if you are about to download any set of records from a database without being making a change on MVC. Of course, you can still use the same coding on MVC also to parse the CSV or to export a CSV data.

CSV Parsing

CSV.foreach(“/home/deploy/apps.csv”) do |row|
orgs << SdmUser.find_by_c_attribute1(row.to_s)
end

CSV Upload 

CSV.open(“/home/deploy/orgs.csv”, “wb”) do |title|

title << [“ORG Name”]

orgs.each do |org|

title << [org.c_attribute1.to_s]

end

end

Advertisements