2009-09-28 6 views
2

다음 코드 블록을 environments.rb에 추가하면 ActiveRecord :: Base가 모듈을 개발 환경에서 확장하지만 테스트 환경에서는 확장하지 않습니다.레일에서 ActiveRecord :: Base 확장이 테스트 환경에서 작동하지 않습니다.

module ModelExtensions 

    def human_name_for(attr_hash) 
    # do something 
    end 

end 

로드 ./script/server 및 ./script/console 개발 환경에 잘 보인다 다음과 같이 모듈이 포함

require "active_record_patch" 
ActiveRecord::Base.send(:extend, ModelExtensions) 

라이브러리 파일입니다. 그러나 테스트 환경에서, 다음과 같은 오류가 발생합니다

솔루션에 대한
/home/test/rails_app/vendor/rails/activerecord/lib/active_record/base.rb:1959:in `method_missing':NoMethodError: undefined method `human_name_for' for #<Class:0x4a8d33> 

답변

1

, 나는 모듈을 수정 lib 디렉토리 파일 자체에 액티브에 모듈 :: 자료 포함 :

module HumanAttributes 

    module ClassMethods 

    def human_name_for(attr_hash) 
     unless attr_hash.nil? 
     @@human_names = attr_hash 

     class << self 
      def human_attribute_name key 
      @@human_names[key.to_sym] || super unless key.nil? 
      end 
     end 
     end 
    end 

    end 

end 

module ActiveRecord 
    class Base 
    extend HumanAttributes::ClassMethods 
    end 
end 

이것은 수를 human_name_for 모든 환경에서 ActiveRecord :: Base에서 확장하는 모든 클래스에서 액세스 할 수 있습니다.

모델 파일 맨 위에 파일이 필요합니다.

0

나를 위해 작동합니다. (: ASD) 다음이 _For ArObject.human_name 작동

include ModelExtensions 
ActiveRecord.extend(ModelExtensions) 

으로 environment.rb에서

module ModelExtensions 

    def human_name_for(attr_hash) 
    # do something 
    end 

end 

관련 문제