Skip to main content

Devise models and Single table inheritance and custom class methods

Gems / Libraries • Asked by John Munyi

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)
    end

     def admin?
        type == "TeamLeader"
      end

  end


  class TeamLeader < User

  end


  class Agent < User

   end

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?) %>
     <ul>
         <li><%= link_to "I am admin" %></li>
    </ul>
  <% 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"
  end 

Login or Create An Account to join the conversation.

Subscribe to the newsletter

Join 27,623+ developers who get early access to new screencasts, articles, guides, updates, and more.

    By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

    More of a social being? We're also on Twitter and YouTube.