How to Export a Ruby Array from My Heroku Console into CSV

How to export a Ruby Array from my Heroku console into CSV?

You can't access your local filesystem from the heroku console. One option is to use Tee. Tee sends the output to both STDOUT and a file, so you can have a local log of everything that was printed.

heroku run console | tee output.txt

Downloading from /tmp folder in Heroku

The /tmp directory on Heroku is exactly that – temporary. Even if you store the file in the /tmp file, it won't be persisted for long enough that any users will be able to access it. Instead, you should look into an integrated storage solution like Amazon AWS.

With that in place, your users should be able to access those CSV files directly from your storage host without needing to tie up any Heroku dynos/resources.

how to export limited number of rows from heroku

Sadly, but there's no way to limit rows with pg:pull. However there's another way to make a backup of DB. Here's a link to Heroku Import/Export Guide. You can use pgbackups tool to create a backup. After that, you can simply download it from Heroku.

Another way could be using pg_dump tool (PostgreSQL tool for creating DB dumps), but, as far as I know, it has no way of limiting number of records. However you can dump a table structure without records with pg_dump, and then fill it by yourself or by using custom script. Also, you can try and save a limited number of records to file using SQL COPY.

Exporting CSV data from Rails

The to_csv is a class method. Meaning its meant to be called like Group.to_csv. You might want to change the method signature to something like Group.to_csv(groups) instead.

 def self.to_csv(groups)
Rails.logger.info "Hello World"
CSV.generate do |csv|
csv << column_names
groups.each do |product|
csv << product.attributes.values_at(*column_names)
end
end
end

Then in show

  def show
# @company is being provided correctly.
@groups = @company.groups
render text: Group.to_csv(@groups)
end

in rails, how to return records as a csv file

There is a plugin called FasterCSV that handles this wonderfully.

CSV - pulling data and writing to another CSV in ruby

This looks suspicious:

nces_ids = School.pluck(:nces_id).uniq
nces_ids_track = nces_ids

Assignment doesn't copy nces_ids array, it just copies the reference. The result is that nces_ids and nces_ids_track are referencing the same array. Later you do this:

if nces_ids.include?(row['ncessch'])
CSV.open(file_name, 'a') do |csv|
#...
nces_ids_track.delete(row['ncessch'])
end
end

but nces_ids and nces_ids_track reference the same array not different arrays as expected.

Perhaps you want to say:

nces_ids = School.pluck(:nces_id).uniq
nces_ids_track = nces_ids.dup
# -----------------------^^^^

so that you have two copies of the array to work with.

Exporting dataset as CSV not returning complete dataset

I resolved this issue! After a bit of poking around, I switched from using a HTML formatted view/xls file to just doing a barebones CSV , formatted in a view, as referenced in this answer. This worked perfectly. Thanks for the troubleshooting ideas everyone!

Rails: Creating a csv file in a rake task - can't access a classes database column_names

You're specifying Baseline.column_names in the first case, but just column_names on your values_at call. That defaults to the main context where no such method exists. It must be called against a model.

Make those two consistent, Baseline is required in both cases.

Convert .json to .csv in ruby

Try something like this:

require 'csv'
require 'json'

csv_string = CSV.generate do |csv|
JSON.parse(File.open("foo.json").read).each do |hash|
csv << hash.values
end
end

puts csv_string


Related Topics



Leave a reply



Submit