开发者

Accessing a Constant Defined in ActiveRecord::Base

I am trying to access the constant VALID_FIND_OPTIONS defined in ActiveRecord::Base (active_record/base.rb Line 2402 Rails 2.3.5).

ActiveRecord::Base::VALID_FIND_OPTIONS

I get the NameError exception.

NameError: uninitialized constant ActiveRecord::Base::VALID_FIND_OPTIONS
开发者_开发问答

I have accessed the class constants in other libraries using the similar syntax before. I am not sure where I am going wrong.


The constant VALID_FIND_OPTIONS was defined inside the anonymous class of ActiveRecord::Base, hence it was not accessible as ActiveRecord::Base::VALID_FIND_OPTIONS

module ActiveRecord
  class Base
    class << self
      # the constant belongs to the scope of the anonymous class
      VALID_FIND_OPTIONS = [..]
    end
  end
end

The constant can be accessed using following syntax:

ActiveRecord::Base.singleton_class::VALID_FIND_OPTIONS


Where is the code that tries to get ActiveRecord::Base::VALID_FIND_OPTIONS?

If you are defining a class before ActiveRecord is loaded, then the constant will not be available.

You can force ActiveRecord to be loaded by requiring it. In some cases, you will have to require rubygems before requiring active_record.

Try requiring them both:

require 'rubygems'
require 'active_record'

# you should now be able to access ActiveRecord::Base::VALID_FIND_OPTIONS
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜