Ask A Question


You’re not receiving notifications from this thread.

Devise models and Single table inheritance and custom class methods

John Munyi asked in Gems / Libraries

Hello there I am in a little fix and cant figure out whats going in, i am attempting to have a devise model User share via STI with 2 other sub-classes . I am able to populate both classes with data via console and I can query the user table and get data from the 2 sub-classes, but if i define a custom method in User.rb and trying to access via current_user its not posibble see code

 class User < ActiveRecord::Base
    scope :agent, -> { where(type: "Agent") }
    scope :team_leader, -> { where(type: "TeamLeader") }
     devise :database_authenticatable, :registerable,
           :recoverable, :rememberable, :trackable, :validatable
    has_many :attendances
    belongs_to :team

     self.inheritance_column = :type

    def self.types
      %w(Agent TeamLeader)

     def admin?
        type == "TeamLeader"


  class TeamLeader < User


  class Agent < User


so when i try to hide some resource from the type Agent I get an error of undefined method admin?


Because you're using Ruby to inherit from User on both the TeamLeader and Agent, you're going to get access to everything already defined in User. It's kinda like how ever controller inherits ApplicationController. You get access to all the methods there.

If you don't want the admin? method to exist inside Agent, you'll want to move it out of User and put it in the TeamLeader class instead. Make sense?


That's exactly what I want but strangely r bough. Can't access the def admin method when I try to hide sine links the agents aren't supposed to see..... I can't find anything wrong with my code.... Or is there anything working currentky


Did something like this.

<% if current_user.admin? %>
<% end %>

It throws admin method doesn't exist it's undefined


So i made some progress and changed the above code to:

   <% elsif current_user.try(:admin?) %>
         <li><%= link_to "I am admin" %></li>
  <% else %>

now the "admin? is undefined" error is no more but still my method isnt being evaluate beacuse that link isnot visible even when A TeamLeader logs in to the App, why so ?

and if i run all these queries on the console they all return the correct value.

  User.find(5).type  #this return the correct type for the that user
  User.all #gives all users
  Agent.create(.......) #creates an agent successfully
  TeamLeader.create(.......) #works too

what i am missing in my user class method admin? that’s making it not be evaluated in :

               <% elsif current_user.try(:admin?) %>

because the only logical explanation is the code below is not being triggered or evaluated when .admin? is passed to the current_user..... or something like that

 def admin?
      type == "TeamLeader"
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 84,111+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.