rails csv generator not reaching the model - ruby-on-rails

I have in my controller a section that generates an array of Song objects. For each song object, I want to output some of it's information to a csv file. I found instructions online and followed them to the best of my ability.
The controller:
def index
#stations = Station.all
#csv crap
#played = []
Station.first.users.each do |u|
u.playlists.where("updated_at > ?", Date.today).each do |p|
#played << p
end
end
#songs = []
if !#played.empty?
#played.each do |pl|
pl.songs.each do |s|
#songs << s
end
end
end
#data = []
#data << "Song"
#data << "Album"
#data << "Artist"
(0...#songs.length).each do |s|
son = #songs[s]
#data << [son.title, Album.find(son.album_id).name, Artist.find(son.artist_id).name]
end
respond_to do |format|
puts "******************\n\n\n\n\n\n\n\n"
puts #songs.inspect
puts "******************\n\n\n\n\n\n\n\n"
format.html
format.csv { send_data #data.to_csv(), filename:"daily_report.csv" }
end
end
The model:
def self.to_csv(options = {})
puts "fuck all"
CSV.generate(options) do |csv|
csv << column_names
options.each do |item|
csv << item.title
csv << item.attributes.values_at(*column_names)
csv << item.attributes.values_at(*column_names)
end
end
end
For some reason, the controller is never using the model to generate the csv, which it should be. I have to extra stuff in the controller to try to make it generate correctly there, but I really want it to generate in the model. Any idea why it never hits the self.to_csv in the model?

Your model's to_csv method isn't being called because you're not calling it - you're calling a to_csv instance method on an array.
You need something along the lines of
ModelClass.to_csv(#songs)
and your to_csv method should be closer to
def self.to_csv(data, options = {})
CSV.generate(options) do |csv|
csv << column_names
data.each do |item|
csv << [item.title]
csv << item.attributes.values_at(*column_names)
csv << item.attributes.values_at(*column_names)
end
end
end
I'm a bit confused about what you're adding to the csv (for example why are the item attributes added twice?) but this should at least produce a csv.

Related

Rails - Export CSV not working as expected (undefined method)

I am doing a CSV Export in Rails App.
The data is returned as a JSON array from the backend API.
But now I am getting error as "undefined method 'to_csv' for #Hash:0x007f149a5ff978" in the export_data method.
I have included require "csv" in the config/application.rb file, and did server restarts.
Not sure what I am doing wrong.
index.html.erb
<button class="btn btn-default">Export to CSV</button>
usage_controller(front end)
def export_data
rest_resource = RestClient::Resource.new( ENV['USAGE_METRICS_API'] + '/get_events_data', :verify_ssl => false )
request = rest_resource.get :Authorization => cookies.signed[:remember_token], :content_type => 'application/json'
#events_data = JSON.parse(request)
respond_to do |format|
format.html
format.csv { send_data #events_data.to_csv }
end
end
def self.to_csv
CSV.generate do |csv|
csv << column_names
all.each do |event|
csv << event.attributes.values_at(*column_names)
end
end
end
Your self.to_csv method doesn't make sense in the controller
You need to move to_csv to you model which will serialize the data into a csv
follow this tutorial to implement this: http://railscasts.com/episodes/362-exporting-csv-and-excel

Ruby on Rails: Validate CSV file

Having followed the RailsCast on importing CSV (http://railscasts.com/episodes/396-importing-csv-and-excel), I am trying to validate that the file being uploaded is a CSV file.
I have used the gem csv_validator to do so, as documented here https://github.com/mattfordham/csv_validator
And so my model looks like this:
class Contact < ActiveRecord::Base
belongs_to :user
attr_accessor :my_csv_file
validates :my_csv_file, :csv => true
def self.to_csv(options = {})
CSV.generate(options) do |csv|
csv << column_names
all.each do |contact|
csv << contact.attributes.values_at(*column_names)
end
end
end
def self.import(file, user)
allowed_attributes = ["firstname","surname","email","user_id","created_at","updated_at", "title"]
CSV.foreach(file.path, headers: true) do |row|
contact = find_by_email_and_user_id(row["email"], user) || new
contact.user_id = user
contact.attributes = row.to_hash.select { |k,v| allowed_attributes.include? k }
contact.save!
end
end
end
But my system still allows me to select to import non-CSV files (such as .xls), and I receive the resulting error: invalid byte sequence in UTF-8.
Can someone please tell me why and how to resolve this?
Please note that I am using Rails 4.2.6
You can create a new class, let's say ContactCsvRowValidator:
class ContactCsvRowValidator
def initialize(row)
#row = row.with_indifferent_access # allows you to use either row[:a] and row['a']
#errors = []
end
def validate_fields
if #row['firstname'].blank?
#errors << 'Firstname cannot be empty'
end
# etc.
end
def errors
#errors.join('. ')
end
end
And then use it like this:
# contact.rb
def self.import(file, user)
allowed_attributes = ["firstname","surname","email","user_id","created_at","updated_at", "title"]
if file.path.split('.').last.to_s.downcase != 'csv'
some_method_which_handle_the_fact_the_file_is_not_csv!
end
CSV.foreach(file.path, headers: true) do |row|
row_validator = ContactCsvRowValidator.new(row)
errors = row_validator.errors
if errors.present?
some_method_to_handle_invaid_row!(row)
return
end
# other logic
end
end
This pattern can easily be modified to fit your needs. For example, if you need to have import on several different models, you could create a base CsvRowValidator to provide basic methods such as validate_fields, initialize and errors. Then, you could create a class inheriting from this CsvRowValidator for each model you want, having its own validations implemented.

How to generate a CSV file in action?

I am trying to generate a CSV output with data from my database. I would like to provide these data to a third party, so I picture I would give to someone a URL (website.com/api_data/cars) and by accessing this URL the person would be able to work with it - I think I want to access the URL and then to see there (in the action) data displayed and separated by , or ;.
But how to do that?
So far, I am trying following approach:
csv_string = CSV.generate do |csv|
cols = ["column one", "column two", "column three"]
csv << cols
csv << ["A", "B", "C"]
#filename = "data-#{Time.now.to_date.to_s}.csv"
end
send_data(csv_string, :type => 'text/csv; charset=utf-8; header=present', :filename => #filename)
This is in the controller generate_data and action csv_cars.
When I run this action (webste.com/generate_data/csv_cars), it will automatically pop up a window to download the file.
But how to write the CSV content to the action? So when I open the URL, I'll see there written the content from the database?
I know this is an old thread but I came across it in my search so in case someone else does the same, here's my answer and what worked for me.
I think bhanu had a good way of going about it but I did change something. Instead of doing #cars within the respond_to, I just called send_data Cars.to_csv since, as Rob stated, it was made as a class method. It worked beautifully for me.
class Car < ActiveRecord::Base
def self.to_csv(make)
attributes = %w{id name price} #customize columns here
cars = Car.where(maker_name: make)
CSV.generate(headers: true) do |csv|
csv << attributes
cars.each do |car|
csv << attributes.map{ |attr| car.send(attr) }
end
end
end
end
And then in the controller
class CarsController < ApplicationController
def index
send_data Cars.to_csv('Chevy'), filename: "cars-#{Date.today}.csv"
end
end
I understand that this will be called when you go to cars/index but you can put that into any method, if statement or anything you want and just have it called whenever you would like from there. You can also have arguments, as I did above with make, and query for certain fields. It was definitely a lot easier than I thought it was going to be. Hope this helped someone.
You need to do something like this.
def csv_cars
headers = ['column one', 'column two', 'column three']
csv_data = CSV.generate(headers: true) do |csv|
csv << headers
csv << ["A", "B", "C"]
end
send_data csv_data, filename: "data-#{Date.today.to_s}.csv", disposition: :attachment
end
define a to_csv method in your model as shown below
class Car < ActiveRecord::Base
def self.to_csv
attributes = %w{id name price} #customize columns here
CSV.generate(headers: true) do |csv|
csv << attributes
all.each do |car|
csv << attributes.map{ |attr| car.send(attr) }
end
end
end
end
Later in your controller
class CarsController < ApplicationController
def index
#cars = Car.all
respond_to do |format|
format.html
format.csv { send_data #cars.to_csv, filename: "cars-#{Date.today}.csv" }
end
end
end

Rails - export to csv not loading values

Im doing an export to CSV in Rails everything seems to be ok no errors and the file is being generated, but only the column headers are been exported no other data.
Here is some of the code:
in controller:
#students = Student.where("START_DATE >= ? and END_DATE <= ? and COURSE_NAME like ?", params[:start_date], params[:end_date], params[:course_name]).select('name, course')
(...)
format.csv { send_data #students .to_csv }
in model:
def self.to_csv(options = {})
CSV.generate(options) do |csv|
csv << column_names
all.each do |valeo|
csv << valeo.attributes.values_at(*column_names)
end
end
end
What I have found as cause of the issue is that when I click on the link
= link_to "Export Records", student_path(:format => :csv)
#students is empty. I don't know why ? or how to solve the issue.

CSV export giving #<Product:0xba4618> and similar entries instead of real data?

I am following this http://railscasts.com/episodes/362-exporting-csv-and-excel and it looks to be pretty straight forward. I have included 'csv' and have put the code in controller index file and in model.
Now when I am clicking on /products.csv, it do export the list of products in csv but instead of having actual data it is having entries like #, what am I doing wrong ?
EDIT - here is the product.rb
attr_accessible :name, :size, :email
def self.to_csv
CSV.generate do |csv|
csv << column_names
all.each do |product|
csv << product.attributes.values_at(*column_names)
end
end
end
In your products controller define a method as below:
def export_csv
#Products = Product.find(:all)
csv = CSV.generate do |csv|
# header row
csv << ["product_name","product_type"]
# data row
#products.each do |t|
csv << [t.product_name, t.product_type]
end
end
send_data(csv, :type => 'text/csv')
end
In your routes:
match 'csv' => 'products#export_csv', :as => :csv
In your view give a link for CSV:
link_to "CSV", csv_path

Resources