[英]Multiple Table Inheritance in Rails 4/Devise setting the current_user
So I have decided to use MTI for different users of my app. 因此,我决定为我的应用程序的不同用户使用MTI。 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.
用户对于我不应该使用STI的信息有足够不同的信息,我希望它在以后的工作中更加强大。 I have an app built in rails 4 using devise that has a polymorphic association for the users, made like this
我有一个使用dev内置在rails 4中的应用,该应用对用户具有多态关联,就像这样
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? 这一切都很好,但我想知道如何将设备的“ current_user”设置为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.
我可以创建一个新的User1并更新用户的:rolable属性,以在控制台中包含对特定User1的引用,但是我不确定将其放在我的代码中的位置。
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.
我当时想我将修改用户控制器以在创建时添加User1模型,然后在应用程序控制器中将current_user设置为User1的实例。
Is this advised or is there a better way to make the current_user? 这是建议还是有更好的方法来使current_user?
A second thought i had was setting another variable and getting the type using the columns in the User class. 我的第二个想法是设置另一个变量,并使用User类中的列获取类型。 (rolable_id and rolable_type)
(rolable_id和rolable_type)
Which would be the best way? 哪种方法最好? (I am not the most experienced Rails Developer)
(我不是最有经验的Rails开发人员)
For my project I did not change the current_user from Devise's instance of current_user. 对于我的项目,我没有从Devise的current_user实例中更改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.
相反,我在需要每个用户时为它们创建了新对象,并使用current_user的id字段作为特定用户的外键。 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.
更改current_user可能不是最好的主意,因为那样我就失去了对User表和该表中信息的引用。 It worked just fine to create an object for each User
为每个用户创建一个对象都很好
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.