简体   繁体   中英

Multiple Table Inheritance in Rails 4/Devise setting the current_user

So I have decided to use MTI for different users of my app. The users have different enough info to where I don't think i should use STI and I wanted it more robust down the road. I have an app built in rails 4 using devise that has a polymorphic association for the users, made like this

class User < ActiveRecord::Base

  belongs_to :rolable, :polymorphic => true

  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable

  devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :trackable, :validatable, :confirmable, :timeoutable
end 

with the other classes looking like

class User1 < ActiveRecord::Base
  has_one :user, :as => :rolable
end

This is all fine and dandy but I am asking how I will set the devise's "current_user" to be an instance of User1? is that possible or recommended? I can make a new User1 and update the :rolable attribute of a user to include a reference to a specific User1 in the console, but I am not sure where to put this in my code.

I want the sign-in information to be the same for all users but I want the user to be able to change user specific info upon login. I was thinking I would modify the user controller to add the User1 model on creation and then in the application controller I would set the current_user to be an instance of User1.

Is this advised or is there a better way to make the current_user?

A second thought i had was setting another variable and getting the type using the columns in the User class. (rolable_id and rolable_type)

Which would be the best way? (I am not the most experienced Rails Developer)

For my project I did not change the current_user from Devise's instance of current_user. instead I created new objects for each user when i needed them and used the current_user's id field as the foreign key for the specific Users. Changing the current_user probably wasn't the best idea because then i lose the reference to the User table and the info in that table. It worked just fine to create an object for each User

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM