Ask A Question

Notifications

You’re not receiving notifications from this thread.

Exporting Records To CSV Discussion

Discussion for Exporting Records To CSV
Anthony Candaele Anthony Candaele

Hi, My .to_csv method looks like this:

def self.to_csv
attributes = %w{stamnummer naam datum vak studierichting }

CSV.generate(headers: true) do |csv|
csv << attributes
all.each do |absence|
csv << [absence.user.ugent_student_id, absence.user.fullname, absence.date.strftime("%d %b %Y"), absence.course.title, absence.study_program]
end
end

The problem I have with this is that when I open the .csv file in Excel, all attributes are placed in one cell, while I expected every attribute to be placed in it's own field.

Do you know ho to implement this?

thanks for your help,

Anthony

Reply
Anthony Candaele Anthony Candaele

I fixed it by using an xls.erb template file, as in the Railscast video (http://railscasts.com/episo... )

Reply

This is a great episode, thanks! I am able to do all of this on the index action in the controller I am using to export a csv but when I try to do this on an action which I have called 'annual' I am getting a 404 error as it is trying to run the .to_csv on the #show controller... The action is

def annual
@year = params[:date][:year]
@surveys = Survey.where("year = ?", @year)

respond_to do |format|
format.html # index.html.erb
format.json { render json: @surveys }
format.csv { send_data @surveys.to_csv, filename: "survey-annual-#{Date.today}.csv"}
end
end

I'm at a loss...

Reply

Hi Chris! Thanks for this Episode... I think would be great if all the screencast have the source code in github, because sometimes the video explanation is focused in some part of the code, and the other part you've previously wrote. I'll hope that in the most recent episodes the code will be up. For now I have and uninitialized constant User::Import error, and I don't know how to solve. I saw code without explanation in your user_controller.rband I think that my problem is in User::Import but I can't solve the problem :(

Reply

Never mind! I solved this issue in the next episode :)

Reply

Thanks so much for this awesome post and episode. I found it really useful for setting up a CSV export for my Users model. I noticed a couple commenters were having a similiar issue when attempting to do an export with has_many relationships. I couldn't seem to get the suggestion that Chris gave Nathan to work for me, so after a couple hours of mucking around, I sorted it out and wrote something up quickly in case it helps anyone else out. So basically, I'm just showing how you can export Users and their potentially many Coupons in a single shot.

http://blog.blackninjadojo.com/ruby/rails/2019/02/06/exporting-rails-models-with-has-many-relationships-to-csv.html

Thanks again,
Shereen

Reply

i have a problem i cant solve.

i have a model and controller for cars, and can export the data i need to a csv, however i have images saved and stored by paperclip. cars has a one to many relationship with the images but i cannt for the life of me figure out how to get the urls for the images from the asset pipeline.

currently i have my csv code in the model for cars and i can get the db data from the images but cant get the url's.

i have no issues creating the csv data from the view or cars controller (Philosophically) other than i cant get it to work. lol

any help is greatly appreciated!

THANKS!
d

Reply

This just helped me 4 years later. Thanks, Chris!

Reply

Hey Chris. Love the tutorial. Used it with ease on Rails 5.2. Now with webpack and Rails 6.0 I seem to be getting some sort of weird caching issue in production. I made some changes to the attributes, and they don't update on server. I even added a record and it still didn't show up!

Any ideas as to what might be going on and how to fix it? I posted a Stack question with some more code here.
https://stackoverflow.com/questions/61150453/compile-in-rails-6-webpacker-doesnt-make-changes-to-send-data-with-methods-defi?noredirect=1#comment108188144_61150453

Your Uppy tutorial was amazing!

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 85,376+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.