Search code examples
ruby-on-railsrubydevise

Devise with multiple models


How do I setup devise with more than one model? I've already tried using rolify and cancan to setup separate roles in my database, but each role has a different way to authenticate themselves to login. For example, a student will have a student_number, and a lecturer will have a username but no student_number. Also there is a bunch of other attributes a lecturer won't have that a student will and vice versa.

I'm new to rails 4.


Solution

  • It looks like classes and inheritance can come handy in this case. What about defining a User mode and the let Student and Lecturer inherit from that class?

    class Student < User
      # student's peculiar attributes
    end
    
    class Lecturer < User
      # lecturer's peculiar attributes
    end
    

    Then you can have two separate controllers and corresponding views. The the login page might have two links to the proper login pages.