Undefined method `all' for ActiveSupport in Rails 4.0.0 app -
i'm new developing rails apps, part here, i've relied heavily on rails generate command make scaffolds entire app. while every other part of app works fine, 1 raises error right after being automatically generated.
the error follows:
nomethoderror in configurationscontroller#index undefined method `all' activesupport::configurable::configuration:class
here code snippet contains said no method call
def index @configurations = configuration.all end
and here said model in question
class configuration < activerecord::base belongs_to :users, class_name: 'user', foreign_key: 'user_id' end
i added belongs_to part... anyway, when ran through http://localhost.localhost:3000/users/1/configurations/ error pops up! (yes, used nested routes)
so hit rails console check configuration.all returns , lo , behold
2.0.0-p247 :004 > configuration.all configuration load (0.3ms) select "configurations".* "configurations" => #<activerecord::relation [#<configuration id: 1, user_id: 1, port: 3000, host: "example.org", annoy: 5000, version: "1", machines: nil, created_at: "2013-08-08 02:15:32", updated_at: "2013-08-08 02:15:32">]>
i'm kind of lost why method works in rails console fails in html view on browser. did wrong?
also, here output on webrick in case looks it
started "/users/1/configurations/" 192.168.1.105 @ 2013-08-08 10:24:59 +0800 processing configurationscontroller#index html parameters: {"user_id"=>"1"} completed 500 internal server error in 1ms nomethoderror (undefined method `all' activesupport::configurable::configuration:class): app/controllers/configurations_controller.rb:8:in `index'
unfortunately, created class name part of rails api (though in different namespace). class happens @ top-level namespace, while rails class nested in activesupport::configurable
namespace. class you'll when reference configuration
depends on referencing code located.
to specify want class @ top-level namespace, can refer class ::configuration
.
here's simplified test demonstrate what's happening:
# let's module & class defined library (e.g. rails) module configurable # analogous activesupport::configurable class configuration # analogous activesupport::configurable::configuration end end class controllerbase # analogous actioncontroller::base # actioncontroller::base includes activesupport::configurable. # can confirm in rails 4 returns true: # actioncontroller::base.included_modules.include? activesupport::configurable include configurable end # class , constant class configuration end class mycontroller < controllerbase # analogous code inside controller. # inheriting gives access constants in parent def wrong_class configuration # oops, wanted top-level end def correct_class ::configuration end end # top-level scope p(top_level_access: configuration.name) # 'configuration' p(nested_access: mycontroller.new.wrong_class.name) # 'configurable::configuration' p(scoped_nested_access: mycontroller.new.correct_class.name) # 'configuration'
edit - reply @maru's comment:
in cases can use configuration.name
fully-namespaced name of class. try @ console.
if try rails.logger.info(configuration.name)
inside controller, you'll see activesupport::configurable::configuration
in log.
Comments
Post a Comment