How to change _type document in Mongoid?

I have the following models inside a Rails application:

class User include Mongoid::Document ... end class Admin < User ... end 

I get the user:

 u = User.find(some_key) 

And try changing _type:

 u._type # => "User" u._type = "Admin" u.save u._type # => "Admin" 

But if I reload the object, it will still be the user:

 u.reload u._type = "User" 

What is the correct way to change this?

+6
ruby-on-rails mongodb mongoid
source share
2 answers

Finished solution using raw MongoDB query:

 users.update( { :"_id" => user.id }, { :"$set" => { :"_type" => "Admin" }}) 
+6
source share

you can also use Model # update_attribute to stay with mongoid:

 user.update_attribute(:_type, "Admin") 
+7
source share

All Articles