I use Devise for user authentication and create to models, student and teacher. Now, I want to use mailboxer. With mailboxer, we just need to add the acts_as_messageable
to both models. However, I'm not quite sure how to setup the controller. Here is my current controller:
class ConversationsController < ApplicationController
before_action :authenticate_student!
before_action :authenticate_teacher!
before_action :get_mailbox
def index
@conversations = @mailbox.inbox.paginate(page: params[:page], per_page: 10)
end
private
def get_mailbox
@mailbox = current_student.mailbox || current_teacher.mailbox
end
end
Is there any way to make devise groupped both student and teacher models into one scope, like tie them up so we can just call "user" to get both models? Any other solution is welcome.
If you have two Devise models, it would be handy to define a custom current_user
method:
# in application_controller.rb
def current_user
if current_student
current_student
else
current_teacher
end
end
helper_method :current_user
As a side note, I would advise you to have a single User model and roles with Rolify + Pundit for authorization. It's better to let Devise at its authentication job, and you can create other models for user-type specific information (i.e a student_profile.rb). That would make your code DRYer and save you a lot of headache.