2017-08-30 1 views
2

Rails 4からRails 5.1へのアップグレード。この実行中のRSpecのテストを持っている:Rails 5.1 - ActiveRecord :: RecordNotUnique。新しい引数の数が間違っています

Failure/Error: let(:exception) { 
ActiveRecord::RecordNotUnique.new("oops", Mysql2::Error) } 

ArgumentError: 
wrong number of arguments (given 2, expected 0..1) 

ActiveRecordの例外の構文はRailsの5に変更されているようですか?私はこれが何であるかはには書かれていないと思う。 'Rails 5の新機能'の記事を読んだ。 Googleは見つからなかったし、ソースコードのあるサイトもなかった。

答えて

2

bundle open rails(バンドラを使用すると仮定します)を使用すると、ソースコードがローカルに表示されます。ディレクトリを移動すると、~/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems(rbenvを使用している場合)のようになります。

このディレクトリを検索すると、説明する定義が見つかります。 Vimのシルバーサーチャーを使った例を検索:

:Ag 'class RecordNotUnique' ~/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems 

/Users/xxjjnn/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/activerecord-4.2.7.1/lib/active_record/errors.rb|98 col 3| class RecordNotUnique < WrappedDatabaseException 
/Users/xxjjnn/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/activerecord-5.1.3/lib/active_record/errors.rb|109 col 3| class RecordNotUnique < WrappedDatabaseException 

ここから私たちはRailsの5.1に方法はもはや二つの引数を取ることを見ることはできません。

# BOTH 
class RecordNotUnique < WrappedDatabaseException 
end 
class WrappedDatabaseException < StatementInvalid 
end 

# 4.2.7.1 
class StatementInvalid < ActiveRecordError 
    def initialize(message = nil, original_exception = nil) 
    if original_exception 
     ActiveSupport::Deprecation.warn("Passing #original_exception is deprecated and has no effect. " \ 
             "Exceptions will automatically capture the original exception.", caller) 
    end 
    super(message || $!.try(:message)) 
    end 
    def original_exception 
    ActiveSupport::Deprecation.warn("#original_exception is deprecated. Use #cause instead.", caller) 
    cause 
    end 
end 
# 5.1.3 
class StatementInvalid < ActiveRecordError 
    def initialize(message = nil) 
    super(message || $!.try(:message)) 
    end 
end 

ソースをgrepをするこの技術は、多くの問題を解決することができますこのような問題。

関連する問題