2016-06-02 5 views
0

カスタム属性バリデーターで奇妙なエラーが発生しました。私は次のセットアップを持っていたし、すべてが(すべてのクラスは、モジュールAPIとV1で包まれている)、期待通りに働いていた:一部のモデルでArgumentErrorが発生します。バリデーターが不明です。バリデーターが見つかりました。

# app/models/api/v1/business_account.rb 
class BusinessAccount 
    has_many :transactions, class_name: BusinessAccountThings::Transaction 
end 

# app/models/api/v1/business_account_things/transaction.rb 
module BusinessAccountThings 
    class Transaction 
    belongs_to :business_account 
    validates :amount, money: true 
    end 
end 

# app/validators/api/v1/money_validator.rb 

class MoneyValidator < ActiveModel::EachValidator 
    def validate_each(record, attribute, value) 
    unless /^\d+\.*\d{0,2}$/ =~ value.to_s 
     record.errors[attribute] << 'Only two digits are allowed.' 
    end 
    end 
end 

私は今、このように、BusinessAccountでカスタムバリデータを使用したい:

class BusinessAccount 
    has_many :transactions 
    validates :current_balance, money: true 
end 

(Railsのコンソールからクラスをロードしようとしたときと同様のエラー)BusinessAccountの仕様を実行しているとき、私は次のエラーを取得:

/home/hannes/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/gems/activemodel-4.2.4/lib/active_model/validations/validates.rb:120:in `rescue in block in validates': Unknown validator: 'MoneyValidator' (ArgumentError) 
    from /home/hannes/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/gems/activemodel-4.2.4/lib/active_model/validations/validates.rb:117:in `block in validates' 
    from /home/hannes/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/gems/activemodel-4.2.4/lib/active_model/validations/validates.rb:113:in `each' 
    from /home/hannes/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/gems/activemodel-4.2.4/lib/active_model/validations/validates.rb:113:in `validates' 
    from /home/hannes/workspaces/rails-app/app/models/api/v1/business_account_things/transaction.rb:22:in `<class:Transaction>' 
    from /home/hannes/workspaces/rails-app/app/models/api/v1/business_account_things/transaction.rb:5:in `<module:BusinessAccountThings>' 
    from /home/hannes/workspaces/rails-app/app/models/api/v1/business_account_things/transaction.rb:3:in `<module:V1>' 
    from /home/hannes/workspaces/rails-app/app/models/api/v1/business_account_things/transaction.rb:2:in `<module:API>' 
    from /home/hannes/workspaces/rails-app/app/models/api/v1/business_account_things/transaction.rb:1:in `<top (required)>' 
    from /home/hannes/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:274:in `require' 
    from /home/hannes/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:274:in `block in require' 
    from /home/hannes/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:240:in `load_dependency' 
    from /home/hannes/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:274:in `require' 
    from /home/hannes/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:360:in `require_or_load' 
    from /home/hannes/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:494:in `load_missing_constant' 
    from /home/hannes/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:184:in `const_missing' 
    from /home/hannes/workspaces/rails-app/app/models/api/v1/business_account.rb:16:in `<class:BusinessAccount>' 
    from /home/hannes/workspaces/rails-app/app/models/api/v1/business_account.rb:4:in `<module:V1>' 
    from /home/hannes/workspaces/rails-app/app/models/api/v1/business_account.rb:2:in `<module:API>' 
    from /home/hannes/workspaces/rails-app/app/models/api/v1/business_account.rb:1:in `<top (required)>' 
    from /home/hannes/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:274:in `require' 
    from /home/hannes/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:274:in `block in require' 
    from /home/hannes/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:240:in `load_dependency' 
    from /home/hannes/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:274:in `require' 
    from /home/hannes/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:360:in `require_or_load' 
    from /home/hannes/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:494:in `load_missing_constant' 
    from /home/hannes/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:184:in `const_missing' 
    from /home/hannes/workspaces/rails-app/spec/models/api/v1/business_account_spec.rb:5:in `<top (required)>' 
    from /home/hannes/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/gems/rspec-core-3.4.4/lib/rspec/core/configuration.rb:1361:in `load' 
    from /home/hannes/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/gems/rspec-core-3.4.4/lib/rspec/core/configuration.rb:1361:in `block in load_spec_files' 
    from /home/hannes/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/gems/rspec-core-3.4.4/lib/rspec/core/configuration.rb:1359:in `each' 
    from /home/hannes/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/gems/rspec-core-3.4.4/lib/rspec/core/configuration.rb:1359:in `load_spec_files' 
    from /home/hannes/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/gems/rspec-core-3.4.4/lib/rspec/core/runner.rb:106:in `setup' 
    from /home/hannes/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/gems/rspec-core-3.4.4/lib/rspec/core/runner.rb:92:in `run' 
    from /home/hannes/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/gems/rspec-core-3.4.4/lib/rspec/core/runner.rb:78:in `run' 
    from /home/hannes/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/gems/rspec-core-3.4.4/lib/rspec/core/runner.rb:45:in `invoke' 
    from /home/hannes/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/gems/rspec-core-3.4.4/exe/rspec:4:in `<top (required)>' 
    from /home/hannes/.rbenv/versions/2.2.5/bin/rspec:23:in `load' 
    from /home/hannes/.rbenv/versions/2.2.5/bin/rspec:23:in `<main>' 

16行01にはhas_many :transactions, class_name: BusinessAccountThings::Transaction, dependent: :destroy

行22のTransactionvalidates :amount, money: trueです。

Transactionから22行目を削除すると、問題は解決します。

私はTransactionのための私のスペックを実行する場合、私はこのエラーを取得する:両方のクラスが同時にMoneyValidatorをロードすることはできませんかのように

1) API::V1::BusinessAccountThings::Transaction 
    Failure/Error: 
    validates :current_balance, 
       numericality: { greater_than_or_equal_to: 0 }, 
       money: true 

ArgumentError: 
    Unknown validator: 'MoneyValidator' 
# ./app/models/api/v1/business_account.rb:9:in `<class:BusinessAccount>' 
# ./app/models/api/v1/business_account.rb:4:in `<module:V1>' 
# ./app/models/api/v1/business_account.rb:2:in `<module:API>' 
# ./app/models/api/v1/business_account.rb:1:in `<top (required)>' 
# ./spec/models/api/v1/business_account_things/transaction_spec.rb:5:in `block (2 levels) in <top (required)>' 
# ./spec/models/api/v1/business_account_things/transaction_spec.rb:7:in `block (2 levels) in <top (required)>' 
# ------------------ 
# --- Caused by: --- 
# NameError: 
# uninitialized constant API::V1::BusinessAccount::MoneyValidator 
# ./app/models/api/v1/business_account.rb:9:in `<class:BusinessAccount>' 

だから、それが表示されます...どのように私はこの問題を解決することができますか?

+0

あなたは失敗している完全なスペックを投稿できますか? –

+0

もちろんできますが、コンソールで同じエラーが発生するので、これは私の仕様とは関係ありません。あなたはまだそれが関連していると思いますか?または、見たいスペックの特定の部分はありますか? 'Transaction'の仕様はかなり短いですが、' BusinessAccount'の仕様は100 LOCを超えています... – ehannes

+0

私には 'API :: V1 :: MoneyValidator'の自動ロードが失敗するようです。 'business_account.rb'はそれを' API :: V1 :: BusinessAccount :: MoneyValidator'としてロードしようとしています。あなたのバリデーターがトップレベルの定数、つまり 'API :: V1'なしであればうまくいくと思います。 – Raffael

答えて

1

オートローディングAPI::V1::MoneyValidatorがセットアップに失敗します。

business_account.rbは、API::V1::BusinessAccount::MoneyValidatorとしてロードしようとしています。それはAPI::V1::BusinessAccountではなく、API::V1の下に名前空間があるため失敗します。

オートローディングでは、MoneyValidator(名前空間なし、ソースコードで書かれているように)も検索されます。したがって、バリデータークラスをAPI::V1名前空間から移動してトップレベルの定数にすると、それはうまくいくはずです。

関連する問題