Hi,
I am trying to read the
file(http://wikisend.com/download/703976/votes.txt)
with the below code :
require ‘csv’
path = ‘/home/votes.txt’
CSV::Converters[:cencoding] = lambda do |string|
string.encode(‘UTF-8’, ‘binary’, invalid: :replace, undef: :replace,
replace: ‘’)
end
CSV.foreach(path, :col_sep => " ", :converters => :cencoding) do |array|
fields = { ‘campaign’ => 2, ‘validity’ => 3, ‘choice’ => 4 }
field_values = fields.each_with_object({}) do |(name, index), hash|
match = /#{name}:(\w+)/i.match(array[index])
hash[name]= match[1].strip unless match.nil?
end
next unless field_values.values.all?(&:present?)
Campaign.create!(name: field_values[‘campaign’]) unless
Campaign.exists?
(name: field_values[‘campaign’])
campaign = Campaign.find_by_name(field_values[‘campaign’])
campaign.votes.create!(validity: field_values[‘validity’], choice:
field_values[‘choice’])
end
But, I’m getting error as -
arup@linux-wzza:~/Rails/tv_sms_voting> rake db:reset
#…
#…
rake aborted!
ArgumentError: invalid byte sequence in UTF-8
/home/arup/Rails/tv_sms_voting/db/seeds.rb:20:in <top (required)>' /home/arup/.rvm/gems/ruby-2.1.2@active_record/gems/activesupport-4.1.4/lib/active_support/dependencies.rb:241:in
load’
/home/arup/.rvm/gems/ruby-2.1.2@active_record/gems/activesupport-4.1.4/lib/active_support/dependencies.rb:241:in
block in load' /home/arup/.rvm/gems/ruby-2.1.2@active_record/gems/activesupport-4.1.4/lib/active_support/dependencies.rb:232:in
load_dependency’
/home/arup/.rvm/gems/ruby-2.1.2@active_record/gems/activesupport-4.1.4/lib/active_support/dependencies.rb:241:in
load' /home/arup/.rvm/gems/ruby-2.1.2@active_record/gems/railties-4.1.4/lib/rails/engine.rb:543:in
load_seed’
/home/arup/.rvm/gems/ruby-2.1.2@active_record/gems/activerecord-4.1.4/lib/active_record/tasks/database_tasks.rb:184:in
load_seed' /home/arup/.rvm/gems/ruby-2.1.2@active_record/gems/activerecord-4.1.4/lib/active_record/railties/databases.rake:173:in
block (2 levels) in <top (required)>’
/home/arup/.rvm/gems/ruby-2.1.2@active_record/gems/activerecord-4.1.4/lib/active_record/railties/databases.rake:132:in
`block (2 levels) in <top (required)>’
Tasks: TOP => db:setup => db:seed
(See full trace by running task with --trace)
arup@linux-wzza:~/Rails/tv_sms_voting>
================
How can I handle this error ?
–
Regards,
Arup R.
Debugging is twice as hard as writing the code in the first place.
Therefore,
if you write the code as cleverly as possible, you are, by definition,
not
smart enough to debug it.
–Brian Kernighan