Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

Michael-James Parsons
Michael-James Parsons
1,774 Points

Rails 4 - Using namespaced models with devise

I asked the following question on StackOverflow, but I it hasn't been answered yet.

My rails app has 5 user models that extend "User". All of which have the namespace "User::UserTypeHere". When I attempt to logout of my application, devise tries to access the models as if they belong to the top level namespace, and I get the following error:

"The single-table inheritance mechanism failed to locate the subclass: 'SuperUser'. This error is raised because the column 'type' is reserved for storing the class in case of inheritance..."

How do I set up my routes in a way that devise will recognize my namespaced models?

(code snippets at: http://stackoverflow.com/questions/31868216/rails-4-using-namespaced-models-with-devise)

2 Answers

Michael-James Parsons
Michael-James Parsons
1,774 Points

My question was answered on stack overflow:

"The value of the type column for your users needs to be Users::SuperUser, instead of just SuperUser, so that Rails autoload can find the correct class path.

Also, remember to restart your rails console & server when changing classes around, and don't keep old user instances with wrong values for type, as that will force rails to trigger an error when finding them".

See Stack Overflow for details: http://stackoverflow.com/questions/31868216/rails-4-using-namespaced-sti-models-with-devise/31898774#31898774

Kevin Korte
Kevin Korte
28,149 Points

Been down this road with STI, this was the thread that answered my question: http://stackoverflow.com/questions/4507149/best-practices-to-handle-routes-for-sti-subclasses-in-rails BTW, my solution that seemed to work was

class Users::SuperUser < User
  def self.model_name
    User.model_name
  end
end

I need to learn more rails myself, but hope this helps you.

Michael-James Parsons
Michael-James Parsons
1,774 Points

Thank you for responding so quickly. Unfortunately, your suggested solution did not resolve the errors I have been receiving from my application.