Question

I have a rails application which uses clearance and cancan gems for authentication and authorization. Initially, the application used User model as the user_model to be used by Clearance. Now, because of some design changes, we want to have another model class Person to be used as a user_model by Clearance. The Person model is already in place and has columns specific to Person model in the context of the application.

Rails version: 3.2.14
Ruby version: 1.9.3-p487
Clearance version: 0.16.3

To achieve this, we have done following changes:

  1. Migrated fields (email, encrypted_password, remember_token, salt, confirmation_token) required by Clearance from User model to Person model.
  2. Made the Person model include Clearance::User
  3. Changed the Clearance initializer to set *user_model = Person* as suggested in http://rubydoc.info/github/thoughtbot/clearance/frames

    Clearance.configure do |config|
      config.user_model = Person
    end
    
  4. Updated factory definition for Person model as follows:

    FactoryGirl.define do
      sequence(:email) {|n| "user#{n}@example.com" }
      factory :person do
        ignore do
          group nil
        end
        email
        password { "password" }
        company
        first_name { FactoryGirl.generate(:name).capitalize }
        last_name  "Test"
        groups { [group || FactoryGirl.create(:group)] }
      end
    end
    

After these changes, when I run specs related to persons, it is not able to create the person object using the factory. I get following issue.

    1) Person 
       Failure/Error: it { should validate_presence_of(:first_name) }
       NameError: undefined local variable or method `encrypted_password' for #<Person:0x00000001e7bcc8>
       # ./spec/models/person_spec.rb:8:in `block (2 levels) in <top (required)>'

I referred the documentation, topics on google groups for clearance group, forum posts but couldn't resolve the issue. Has anyone done similar? Can you please help me resolve this issue?

No correct solution

OTHER TIPS

It looks like your Person class does not have the encrypted_password column. Be sure it has this, and also the fields below

t.string :email
t.string :encrypted_password, :limit => 128
t.string :confirmation_token, :limit => 128
t.string :remember_token, :limit => 128

If you post your schema.rb or person.rb file this can be confirmed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top