Michael Hartl's rails tutorial: reusing sessions resource for multiple User types

Posted by ntaj on Stack Overflow See other posts from Stack Overflow or by ntaj
Published on 2012-06-04T03:35:30Z Indexed on 2012/06/04 4:40 UTC
Read the original article Hit count: 627

Filed under:

I'm learning Ruby on Rails through Micheal Hartl's tutorial.

In my web application, I require two types of Users, called publishers and subscribers. Currently I have a publishers resource and a subscribers resource. I'd like both of them to be able to use the sessions resource for signin/signout.

I'm looking for some direction on how to implement this. Should my publisher and subscriber resource inherit from the User resource, so that, for example, the following code works through polymorphism?

def create
  user = User.find_by_email(params[:session][:email])
  if user && user.authenticate(params[:session][:password])
    # Sign the user in and redirect to the user's show page.
  else
    # Create an error message and re-render the signin form.
  end
end

The publisher and subscriber do not have many common fields and different capabilities, so I think they should be modeled as two separate resources, but how to have them share the Sessions resource?

© Stack Overflow or respective owner

Related posts about ruby-on-rails