#20 ✓resolved
scottnj

problem using alternate user model name

Reported by scottnj | January 1st, 2009 @ 10:46 PM

I am trying to setup an app with a UserAccount model instead of User. Whenever I try to save a new user account, I get "NameError: uninitialized constant UserAccount::UserAccountSession".

I am not sure if the problem is with my code or a bug in authlogic. -Scott

models/user_account.rb


class UserAccount < ActiveRecord::Base
  acts_as_authentic :session_clas => "UserSession"
end

models/user_session.rb


class UserSession < Authlogic::Session::Base
  authenticate_with UserAccount
end

> ruby script/console
>> UserSession.authenticate_with(UserAccount) 

=> UserAccount(
       id: integer, 
       username: string, 
       email: string, 
       email_verify_token: string, 
       email_verify_token_created_at: datetime, 
       password_reset_token: string, 
       password_reset_token_created_at: datetime, 
       crypted_password: string, 
       password_salt: string, 
       persistence_token: string, 
       login_count: integer, 
       last_request_at: datetime, 
       last_login_at: datetime, 
       current_login_at: datetime, 
       last_login_ip: string, 
       current_login_ip: string, 
       created_at: datetime, 
       updated_at: datetime)

>> UserAccount.acts_as_authentic_config

=> {
       :validate_password_field=>true, 
       :validate_fields=>true,
       :crypto_provider=>Authlogic::CryptoProviders::Sha512,
       :email_field=>:email, 
       :login_field_type=>:login,
       :password_field_validates_length_of_options=>{},
       :perishable_token_field=>:password_reset_token,
       :single_access_token_field=>nil, 
       :session_class=>"UserAccountSession",
       :email_field_validates_length_of_options=>{}, 
       :session_ids=>[nil], 
       :login_field=>:username, 
       :transition_from_crypto_provider=>[],
       :password_field_validates_confirmation_of_options=>{},
       :perishable_token_valid_for=>600,
       :email_field_validates_format_of_options=>{},
       :login_field_validates_length_of_options=>{},
       :login_field_validation_options=>{}, 
       :logged_in_timeout=>600, 
       :persistence_token_field=>:persistence_token,
       :session_clas=>"UserSession", 
       :password_salt_field=>:password_salt,
       :email_field_validates_uniqueness_of_options=>{},
       :password_field_validation_options=>{},
       :login_field_validates_format_of_options=>{}, 
       :validation_options=>{},
       :password_confirmation_field_validates_presence_of_options=>{},
       :email_field_validation_options=>{},
       :login_field_validates_uniqueness_of_options=>{},
       :crypted_password_field=>:crypted_password, 
       :validate_email_field=>true, 
       :validate_login_field=>true, 
       :password_field=>:password}

>> b = UserAccount.new(
       :username => "jack", 
       :email => "jack@example.com", 
       :password => "123456", 
       :password_confirmation => "123456")

=> #<UserAccount 
       id: nil, 
       username: "jack", 
       email: "jack@example.com", 
       email_verify_token: nil, 
       email_verify_token_created_at: nil, 
       password_reset_token: nil, 
       password_reset_token_created_at: nil, 
       crypted_password: "8deaf951f3a5b4e05214f697a7859d4186eb0181638c8c2ad11...", 
       password_salt: "152b9e23cd412bc96a2adef288d4b3c8d57ae974e8ca73020e8...",
       persistence_token: "6753494162ef0131b0cfa74e19cce05182cb0b04b111af2721b...", 
       login_count: nil, 
       last_request_at: nil, 
       last_login_at: nil, 
       current_login_at: nil, 
       last_login_ip: nil, 
       current_login_ip: nil, 
       created_at: nil, 
       updated_at: nil
       >
>> b.save
NameError: uninitialized constant UserAccount::UserAccountSession
	from c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/active_support/dependencies.rb:102:in `const_missing'
	from C:/Documents and Settings/Scott/Desktop/webdev/rails/try_again/vendor/plugins/authlogic/lib/authlogic/orm_adapters/active_record_adapter/acts_as_authentic/session_maintenance.rb:32:in `update_sessions?'
	from c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/active_support/callbacks.rb:178:in `send'
	from c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/active_support/callbacks.rb:178:in `evaluate_method'
	from c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/active_support/callbacks.rb:196:in `should_run_callback?'
	from c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/active_support/callbacks.rb:166:in `call'
	from c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/active_support/callbacks.rb:93:in `run'
	from c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/active_support/callbacks.rb:92:in `each'
	from c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/active_support/callbacks.rb:92:in `send'
	from c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/active_support/callbacks.rb:92:in `run'
	from c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/active_support/callbacks.rb:277:in `run_callbacks'
	from c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.2.2/lib/active_record/callbacks.rb:315:in `callback'
	from c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.2.2/lib/active_record/callbacks.rb:221:in `create_or_update'
	from c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.2.2/lib/active_record/base.rb:2383:in `save_without_validation'
	from c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.2.2/lib/active_record/validations.rb:1009:in `save_without_dirty'
	from c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.2.2/lib/active_record/dirty.rb:79:in `save_without_transactions'
	from c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.2.2/lib/active_record/transactions.rb:179:in `send'
	from c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.2.2/lib/active_record/transactions.rb:179:in `with_transaction_returning_status'
	from c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.2.2/lib/active_record/connection_adapters/abstract/database_statements.rb:66:in `transaction'
	from c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.2.2/lib/active_record/transactions.rb:129:in `transaction'
	from c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.2.2/lib/active_record/transactions.rb:138:in `transaction'
	from c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.2.2/lib/active_record/transactions.rb:178:in `with_transaction_returning_status'
	from c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.2.2/lib/active_record/transactions.rb:146:in `save'
	from c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.2.2/lib/active_record/transactions.rb:158:in `rollback_active_record_state!'
	from c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.2.2/lib/active_record/transactions.rb:146:in `save'
	from (irb):4>> exit

Comments and changes to this ticket

  • Ben Johnson

    Ben Johnson January 1st, 2009 @ 10:47 PM

    • State changed from “new” to “resolved”

    The name of the user session model is based off of the object you are authenticating with. So rename UserSession to UserAccountSession and you should be good.

  • scottnj

    scottnj January 1st, 2009 @ 10:58 PM

    Actually, I found a typo in my code. I had ":session_clas" which was missing an "s".

    This works as expected:

    
    acts_as_authentic :session_class => "UserSession"
    

    Thanks for the quick reply, Scott

  • jimmy1

    jimmy1 December 30th, 2018 @ 04:22 PM

    Thanks for sharing.I found a lot of interesting information here. A really good post, very thankful and hopeful that you will write many more posts like this one.

    quickpayportal

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป

Object based authentication solution that handles all of the non sense for you. It's as easy as ActiveRecord is with a database.

People watching this ticket

Pages