更改ActiveRecord类的类型Rails的单表继承 [英] Changing type of ActiveRecord Class in Rails with Single Table Inheritance
问题描述
我有两种类型的类:
BaseUser < ActiveRecord::Base
和
User < BaseUser
这acts_as_authentic使用Authlogic的认证系统。这种继承是使用实现单表继承
which acts_as_authentic using Authlogic's authentication system. This inheritance is implemented using Single Table Inheritance
如果一个新的用户注册,我他注册一个用户。不过,如果我已经有相同的电子邮件中的BaseUser,我想这BaseUser更改为数据库中的用户,而不只是在复制所有数据从BaseUser用户,并创建一个新用户(即用新标识)。这可能吗?谢谢你。
If a new user registers, I register him as a User. However, if I already have a BaseUser with the same email, I'd like to change that BaseUser to a User in the database without simply copying all the data over to the User from the BaseUser and creating a new User (i.e. with a new id). Is this possible? Thanks.
推荐答案
您可以只设置类型字段,以用户,并保存记录。内存中的对象仍然显示为一个BaseUser但下一次重新载入内存中的对象将是一个用户
You can just set the type field to 'User' and save the record. The in-memory object will still show as a BaseUser but the next time you reload the in-memory object will be a User
>> b=BaseUser.new
>> b.class # = BaseUser
# Set the Type. In-Memory object is still a BaseUser
>> b.type='User'
>> b.class # = BaseUser
>> b.save
# Retrieve the records through both models (Each has the same class)
>> User.find(1).class # = User
>> BaseUser.find(1).class # User
这篇关于更改ActiveRecord类的类型Rails的单表继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!