mongoid/mongoid — Issue #1496: Type casting fails when the database has been edited from elsewhere
Author: johanlindblad
Created: 2011-12-13 15:07:03+00:00
State: open

I have found that if I for example declare a symbol field, it will be retrieved from the database as a symbol just fine, until you edit the document with an editor like RockMongo, MongoHub or PHPMoAdmin, in which case it turns into a string. This happens even if you don't really change anything, but just click edit and then save.

I just tried this with a new Rails 3.1.3 app on Ruby 1.9.3 on OS X:

1.9.3p0 :001 > Profile.create(:gender => :male)
=> #
1.9.3p0 :002 > Profile.first.gender
=> :male

Then I opened the document in Rockmongo and MongoHub (on separate occasions) and just re-saved the document without changing anything.

Then I tried this:

1.9.3p0 :003 > Profile.first.gender
=> "male"

Here is my model code:

class Profile
include Mongoid::Document
field :gender, :type => Symbol
end

This in turn causes lots of trouble for plugins like symbolize which require that symbol fields really are symbols, or else validation will fail.