ruby on rails - How to upload CSV with Paperclip -
i have looked on other posts creating csv paperclip still bit lost on why isn't working. have method generates csv string (using csv.generate), , try save report in reports controller following method:
def create(type) case type when "geo" csv_string = report.generate_geo_report end @report = report.new(type: type) @report.csv_file = stringio.new(csv_string) if @report.save puts @report.csv_file_file_name else @report.errors.full_messages.to_sentence end end
however, upon execution, undefined method 'stringify_keys' "geo":string
error. here report model:
class report < activerecord::base attr_accessible :csv_file, :type has_attached_file :csv_file, paperclip_options.merge( :default_url => "//s3.amazonaws.com/production-recruittalk/media/avatar-placeholder.gif", :styles => { :"259x259" => "259x259^" }, :convert_options => { :"259x259" => "-background transparent -auto-orient -gravity center -extent 259x259" } ) def self.generate_geo_report male_count = 0 female_count = 0 csv_string = csv.generate |csv| csv << ["first name", "last name", "email", "gender", "city", "state", "school", "created at", "updated at"] athlete.all.sort_by{ |a| a.id }.each |athlete| first_name = athlete.first_name || "" last_name = athlete.last_name || "" email = athlete.email || "" if !athlete.sports.blank? if athlete.sports.first.name.split(" ", 2).first.include?("women's") gender = "female" female_count += 1 else gender = "male" male_count += 1 end else gender = "" end city = athlete.city_id? ? athlete.city.name : "" state = athlete.state || "" school = athlete.school_id? ? athlete.school.name : "" created_at = "#{athlete.created_at.to_date.to_s[0..10].gsub(" ", "0")} #{athlete.created_at.to_s.strip}" updated_at = "#{athlete.updated_at.to_date.to_s[0..10].gsub(" ", "0")} #{athlete.updated_at.to_s.strip}" csv << [first_name, last_name, email, gender, city, state, school, created_at, updated_at] end csv << [] csv << [] csv << ["#{male_count}/#{athlete.count} athletes men"] csv << ["#{female_count}/#{athlete.count} athletes women"] csv << ["#{athlete.count-male_count-female_count}/#{athlete.count} athletes have not declared gender"] end return csv_string end end
this being called cron job rake task:
require 'csv' namespace :reports desc "geo-report" task :generate_nightly => :environment report.create("geo") end end
not sure begin on getting functional. suggestions? i've been reading paperclip's doc i'm bit of newbie it.
thank you!
there's lot going on here :)
first, looks you're getting controller , model confused. in rake task, report
model, you're calling create if controller method. models (aka activerecord classes) take key/value pair:
report.create(type: "geo")
another issue you're using "type" name of column, , tell activerecord you're using single table inheritance. means have subclasses of report. unless want sti, should rename column.
finally, shouldn't have controller method takes argument. i'm not sure you're trying there, controller arguments via params hash.
Comments
Post a Comment