异常日志
CSV: invalid byte sequence in UTF-8
解决前的代码:
rows = CSV.read file_path, col_sep: ';'
rows.each do |row|
puts row.inspect
end
解决方法: 增加 encoding 选项
rows = CSV.read file_path, col_sep: ';', encoding: 'windows-1251:utf-8'
Rake Task: 同一个 task 跑了两次
解决前:
desc 'debug commission report'
task :og_one_report => [:environment] do
do_something
...
end
$ bundle exe rake debug:og_one_report # 日志显示 debug:og_one_report 跑了两次
解决方法, 加一个 exit
desc 'debug commission report'
task :og_one_report => [:environment] do
do_something
...
exit
end
Date: undefined method `striftime’ for :Date
很明显方法拼错了, 应该是 strftime
而不是 striftime
, 这导致了一个线上错误
a.next_date = (a.start_date.striftime('%Y-%m-') + a.active_date.to_s).to_date
修复后:
a.next_date = (a.start_date.strftime('%Y-%m-') + a.active_date.to_s).to_date
Constant: uninitialized constant AutomateCommissionService::OgOneRecord (NameError)
出问题的代码类似于,
class PBar
end
module Foo
class A
end
class B
end
end
class Bar < PBar
def call
begin
2.times { puts A.new }
puts B.new
end
end
end
Bar.new.call # 会丢出 uninitialized constant 的错误
修复方法: 在 Bar 内执行一个 include Foo 操作, 或者使用 Foo::A, Foo::B 来引用 A, B
class Bar < PBar
include Foo
def call
begin
2.times { puts A.new }
puts B.new
end
end
end
通过 puts Bar.ancestors
可以打印出 Bar 的所有祖先,Bar 的祖先也包括了 Bar 自己,
# Bar.ancestors
Bar
Foo
PBar
Object
Kernel
BasicObject
Bar 首先在自己内部查询 A, B 两个常量,如果没有找到就到上一级祖先 Foo 中去查找,依次类推,如果 在最顶级的 BasicObject 里仍然没有找到 A, B 两个常量,就会抛出 uninitialized constant 异常。
nil can’t be coerced into Float
问题解决前的代码大概如下:
module Report
class SummaryRecord
def sum_of_total_local
[
sum_of_dualteam_local,
sum_of_retail_local,
sum_of_fasttrack_local
].reduce(:+)
end
end
end
module Report
class ItalySummaryRecord < SummaryRecord
attr_reader :sum_of_total_local
def payment_rate
payment_after_tax / sum_of_total_local
end
end
end
Report::ItalySummaryRecord#payment_rate
执行时会抛出 nil can't be coerced into Float
的异常,原因是 sum_of_tatal_local
是 nil, 我本意需要 Report::ItalySummaryRecord#payment_rate
执行时会调用其父类 Report::SummaryRecord
的 payment_rate
方法, 某种原因我无法去掉 attr_reader :sum_of_total_local
这段代码,所以我需要在 Report::ItalySummaryRecord
中移除 sum_of_total_local
方法,要达到此目的可以使用 remove_method
, 修复后的代码如下,
module Report
class ItalySummaryRecord < SummaryRecord
attr_reader :sum_of_total_local
remove_method :sum_of_total_local
def payment_rate
payment_after_tax / sum_of_total_local
end
end
end
invalid gem: package is corrupt, exception while verifying: undefined method `size’ for nil:NilClass
安装 rails-4.2.0 时报的一个错误,
gem install rails -v 4.2.0
将 ~/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/cache/activesupport-4.2.0.gem 删除,重新安装就好了。
Capistrano, no such file to load – config/deploy
在生成服务器上部署, 执行:
$ cap deploy
出现错误:
cap aborted!
no such file to load -- config/deploy
解决方法:
$ bundle exe cap deploy