I have Users model in my app with something like
belongs_to :account, :polymorphic => true, :dependent => :destroy
And some special account types like Admin or Editor
class Admin < ActiveRecord::Base
has_one :user, :as => :account, :dependent => :destroy
end
class Editor < ActiveRecord::Base
has_one :user, :as => :account, :dependent => :destroy
end
Is there any clean method to change User type from Editor to Admin? If I do something like
admin = Admin.new
User.first.account = admin
user.save
admin.save
There is still an old entry for editor type in the database.
Using
:dependent => :destroywill only delete the associated object when the object itself is destroyed. You aren’t destroying the user record, just changing one of its attributes, so this functionality is never triggered.To destroy it yourself, do something like