2
votes
/app
  /controllers
    /users
      profiles_controller.rb
      users_controller.rb
  /models
    /user
      /profile.rb
    user.rb


#/app/models/user.rb
class User < ActiveRecord::Base
  has_one :profile, class_name: "User::Profile"
end

#/app/models/user/profile.rb
class User::Profile < ActiveRecord::Base
  belongs_to :user
end

#/app/controllers/users/profiles_controller.rb
class Users::ProfilesController < ApplicationController
  layout "layouts/profile"
end

#/app/controllers/users/users_controller.rb
class Users::UsersController < Users::ProfilesController
  def show
    @user = User.find(params[:id])
  end
end

I have no errors, when accessing to Profile model in console:

vagrant@lucid32:/vagrant$ rails c
Loading development environment (Rails 3.2.11)
irb(main):001:0> u = User.new
=> <#User id: nil, created_at: nil, updated_at: nil, username: nil, crypted_password: nil, password_salt: nil, persistence_token: nil, perishable_token: nil, activate_token: nil, login_count: 0, failed_login_count: 0, activated: false, banned: false, last_request_at: nil, last_login_at: nil, current_login_at: nil, last_login_ip: nil, current_login_ip: nil>
irb(main):002:0> u.build_profile
   (0.5ms)  BEGIN
   (0.4ms)  COMMIT
=> <#User::Profile id: nil, user_id: nil, person: nil, company: nil, company_position: nil, info: nil, birthday: nil, created_at: nil, updated_at: nil>

I got following error when trying access to user/users#show.

LoadError in Users::UsersController#show

Expected /vagrant/app/models/user/profile.rb to define Profile

activesupport (3.2.11) lib/active_support/dependencies.rb:503:in `load_missing_constant'
activesupport (3.2.11) lib/active_support/dependencies.rb:192:in `block in const_missing'
activesupport (3.2.11) lib/active_support/dependencies.rb:190:in `each'
activesupport (3.2.11) lib/active_support/dependencies.rb:190:in `const_missing'
activesupport (3.2.11) lib/active_support/dependencies.rb:514:in `load_missing_constant'
activesupport (3.2.11) lib/active_support/dependencies.rb:192:in `block in const_missing'
activesupport (3.2.11) lib/active_support/dependencies.rb:190:in `each'
activesupport (3.2.11) lib/active_support/dependencies.rb:190:in `const_missing'
activesupport (3.2.11) lib/active_support/inflector/methods.rb:230:in `block in constantize'
activesupport (3.2.11) lib/active_support/inflector/methods.rb:229:in `each'
activesupport (3.2.11) lib/active_support/inflector/methods.rb:229:in `constantize'
activesupport (3.2.11) lib/active_support/inflector/methods.rb:260:in `safe_constantize'
activesupport (3.2.11) lib/active_support/core_ext/string/inflections.rb:66:in `safe_constantize'
actionpack (3.2.11) lib/action_controller/metal/params_wrapper.rb:152:in `_default_wrap_model'
actionpack (3.2.11) lib/action_controller/metal/params_wrapper.rb:169:in `_set_wrapper_defaults'
actionpack (3.2.11) lib/action_controller/metal/params_wrapper.rb:133:in `inherited'
actionpack (3.2.11) lib/abstract_controller/railties/routes_helpers.rb:7:in `block (2 levels) in with'
actionpack (3.2.11) lib/action_controller/railties/paths.rb:7:in `block (2 levels) in with'
app/controllers/users/profiles_controller.rb:1:in `'
activesupport (3.2.11) lib/active_support/dependencies.rb:469:in `load'
activesupport (3.2.11) lib/active_support/dependencies.rb:469:in `block in load_file'
activesupport (3.2.11) lib/active_support/dependencies.rb:639:in `new_constants_in'
activesupport (3.2.11) lib/active_support/dependencies.rb:468:in `load_file'
activesupport (3.2.11) lib/active_support/dependencies.rb:353:in `require_or_load'
activesupport (3.2.11) lib/active_support/dependencies.rb:502:in `load_missing_constant'
activesupport (3.2.11) lib/active_support/dependencies.rb:192:in `block in const_missing'
activesupport (3.2.11) lib/active_support/dependencies.rb:190:in `each'
activesupport (3.2.11) lib/active_support/dependencies.rb:190:in `const_missing'
app/controllers/users/users_controller.rb:1:in `'
activesupport (3.2.11) lib/active_support/dependencies.rb:469:in `load'
activesupport (3.2.11) lib/active_support/dependencies.rb:469:in `block in load_file'
activesupport (3.2.11) lib/active_support/dependencies.rb:639:in `new_constants_in'
activesupport (3.2.11) lib/active_support/dependencies.rb:468:in `load_file'
activesupport (3.2.11) lib/active_support/dependencies.rb:353:in `require_or_load'
activesupport (3.2.11) lib/active_support/dependencies.rb:502:in `load_missing_constant'
activesupport (3.2.11) lib/active_support/dependencies.rb:192:in `block in const_missing'
activesupport (3.2.11) lib/active_support/dependencies.rb:190:in `each'
activesupport (3.2.11) lib/active_support/dependencies.rb:190:in `const_missing'
activesupport (3.2.11) lib/active_support/inflector/methods.rb:230:in `block in constantize'
activesupport (3.2.11) lib/active_support/inflector/methods.rb:229:in `each'
activesupport (3.2.11) lib/active_support/inflector/methods.rb:229:in `constantize'
activesupport (3.2.11) lib/active_support/dependencies.rb:554:in `get'
actionpack (3.2.11) lib/action_dispatch/routing/route_set.rb:69:in `controller_reference'
actionpack (3.2.11) lib/action_dispatch/routing/route_set.rb:54:in `controller'
actionpack (3.2.11) lib/action_dispatch/routing/route_set.rb:32:in `call'
journey (1.0.4) lib/journey/router.rb:68:in `block in call'
journey (1.0.4) lib/journey/router.rb:56:in `each'
journey (1.0.4) lib/journey/router.rb:56:in `call'
actionpack (3.2.11) lib/action_dispatch/routing/route_set.rb:601:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'
rack (1.4.5) lib/rack/etag.rb:23:in `call'
rack (1.4.5) lib/rack/conditionalget.rb:25:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/head.rb:14:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/params_parser.rb:21:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/flash.rb:242:in `call'
rack (1.4.5) lib/rack/session/abstract/id.rb:210:in `context'
rack (1.4.5) lib/rack/session/abstract/id.rb:205:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/cookies.rb:341:in `call'
activerecord (3.2.11) lib/active_record/query_cache.rb:64:in `call'
activerecord (3.2.11) lib/active_record/connection_adapters/abstract/connection_pool.rb:479:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/callbacks.rb:28:in `block in call'
activesupport (3.2.11) lib/active_support/callbacks.rb:405:in `_run__1047939203__call__223494719__callbacks'
activesupport (3.2.11) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.11) lib/active_support/callbacks.rb:385:in `_run_call_callbacks'
activesupport (3.2.11) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.2.11) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/reloader.rb:65:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/remote_ip.rb:31:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/debug_exceptions.rb:16:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
railties (3.2.11) lib/rails/rack/logger.rb:32:in `call_app'
railties (3.2.11) lib/rails/rack/logger.rb:16:in `block in call'
activesupport (3.2.11) lib/active_support/tagged_logging.rb:22:in `tagged'
railties (3.2.11) lib/rails/rack/logger.rb:16:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/request_id.rb:22:in `call'
rack (1.4.5) lib/rack/methodoverride.rb:21:in `call'
rack (1.4.5) lib/rack/runtime.rb:17:in `call'
activesupport (3.2.11) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
rack (1.4.5) lib/rack/lock.rb:15:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/static.rb:62:in `call'
railties (3.2.11) lib/rails/engine.rb:479:in `call'
railties (3.2.11) lib/rails/application.rb:223:in `call'
railties (3.2.11) lib/rails/railtie/configurable.rb:30:in `method_missing'
rack (1.4.5) lib/rack/lint.rb:48:in `_call'
rack (1.4.5) lib/rack/lint.rb:36:in `call'
rack (1.4.5) lib/rack/showexceptions.rb:24:in `call'
rack (1.4.5) lib/rack/commonlogger.rb:33:in `call'
rack (1.4.5) lib/rack/chunked.rb:43:in `call'
rack (1.4.5) lib/rack/content_length.rb:14:in `call'
unicorn (4.6.2) lib/unicorn/http_server.rb:552:in `process_client'
unicorn (4.6.2) lib/unicorn/http_server.rb:632:in `worker_loop'
unicorn (4.6.2) lib/unicorn/http_server.rb:500:in `spawn_missing_workers'
unicorn (4.6.2) lib/unicorn/http_server.rb:142:in `start'
unicorn (4.6.2) bin/unicorn:126:in `'
/usr/local/rbenv/versions/1.9.3-p194/bin/unicorn:23:in `load'
/usr/local/rbenv/versions/1.9.3-p194/bin/unicorn:23:in `'

I don't know exactly what is the reason, but my opinion is:

UsersController in method _default_wrap_model trying get access to Profile model (without namespace) and rise the error. Because when I rename Profile model(example: Info) I have no errors and all is fine.

So my question is: How to avoid this error?

1

1 Answers

1
votes

Problem was because I added directory with namespaced models to autoload_paths:

#/config/application.rb
config.autoload_paths += %W(#{Rails.root}/app/models/user)

When I removed it, all goes fine.