Mongoid returning hash instead of object

896 views
Skip to first unread message

midnightcowboy

unread,
Aug 12, 2011, 11:36:22 AM8/12/11
to Mongoid
I upgraded an existing app from Rails 3.0.5 and Mongoid 2.0 beta to
Rails 3.0.9 and Mongoid 2.1 and I'm having the weird problem that all
my queries now seem to return hashes instead of objects and I can't
figure out where the problem is.

Previously this worked:

User.where(:username => "test").first.name

Now only this works:

User.where(:username => "test").first['name']

I tried different query methods and criteria but always the same
result.

I'm fairly new to Rails and Mongoid, so apologies if it's a silly
mistake on my end. Just wanted to make sure it's not some new default
behaviour that I missed in the changelog.

Durran Jordan

unread,
Aug 12, 2011, 11:45:27 AM8/12/11
to mon...@googlegroups.com
That still works as expected, nothing has changed... What other gems are there? It seems like something else may be hacking the criteria and returning the raw results from the cursor instead of instantiating the documents...

Can you tell me what these evaluate to?:

User.where(:username => "test").collection
User.where(:username => "test").context

2011/8/12 midnightcowboy <matthia...@gmail.com>

midnightcowboy

unread,
Aug 13, 2011, 8:28:25 AM8/13/11
to Mongoid
> User.where(:username => "test").collection

nil


> User.where(:username => "test").context

#<Mongoid::Contexts::Mongo:0x00000102e1f188
@criteria=#<Mongoid::Criteria
selector: {:username=>"test"},
options: {},
class: User,
embedded: false>
>


Here's the contents of my Gemfile and Gemfile.lock, but even when
taking out the 2 groups of test and dev gems, it is still the same.

My Gemfile:

source 'http://rubygems.org'

gem 'rails', '3.0.9'
gem "mongoid", "~> 2.1"
gem "bson_ext", "~> 1.3"
gem 'bcrypt-ruby'
gem 'mail'

group :test, :development do
gem 'rspec-rails'
gem 'guard'
gem 'guard-rspec'
gem 'rb-fsevent'
end

group :test do
gem 'mongoid-rspec'
gem 'factory_girl_rails'
gem 'cucumber-rails'
end


My Gemfile.lock

GEM
remote: http://rubygems.org/
specs:
abstract (1.0.0)
actionmailer (3.0.9)
actionpack (= 3.0.9)
mail (~> 2.2.19)
actionpack (3.0.9)
activemodel (= 3.0.9)
activesupport (= 3.0.9)
builder (~> 2.1.2)
erubis (~> 2.6.6)
i18n (~> 0.5.0)
rack (~> 1.2.1)
rack-mount (~> 0.6.14)
rack-test (~> 0.5.7)
tzinfo (~> 0.3.23)
activemodel (3.0.9)
activesupport (= 3.0.9)
builder (~> 2.1.2)
i18n (~> 0.5.0)
activerecord (3.0.9)
activemodel (= 3.0.9)
activesupport (= 3.0.9)
arel (~> 2.0.10)
tzinfo (~> 0.3.23)
activeresource (3.0.9)
activemodel (= 3.0.9)
activesupport (= 3.0.9)
activesupport (3.0.9)
arel (2.0.10)
bcrypt-ruby (2.1.4)
bson (1.3.1)
bson_ext (1.3.1)
builder (2.1.2)
capybara (1.0.1)
mime-types (>= 1.16)
nokogiri (>= 1.3.3)
rack (>= 1.0.0)
rack-test (>= 0.5.4)
selenium-webdriver (~> 2.0)
xpath (~> 0.1.4)
childprocess (0.2.1)
ffi (~> 1.0.6)
cucumber (1.0.2)
builder (>= 2.1.2)
diff-lcs (>= 1.1.2)
gherkin (~> 2.4.5)
json (>= 1.4.6)
term-ansicolor (>= 1.0.5)
cucumber-rails (1.0.2)
capybara (>= 1.0.0)
cucumber (~> 1.0.0)
nokogiri (>= 1.4.6)
diff-lcs (1.1.2)
erubis (2.6.6)
abstract (>= 1.0.0)
factory_girl (2.0.3)
factory_girl_rails (1.1.0)
factory_girl (~> 2.0.0)
railties (>= 3.0.0)
ffi (1.0.9)
gherkin (2.4.6)
json (>= 1.4.6)
guard (0.5.1)
thor (~> 0.14.6)
guard-rspec (0.4.1)
guard (>= 0.4.0)
i18n (0.5.0)
json (1.5.3)
json_pure (1.5.3)
mail (2.2.19)
activesupport (>= 2.3.6)
i18n (>= 0.4.0)
mime-types (~> 1.16)
treetop (~> 1.4.8)
mime-types (1.16)
mongo (1.3.1)
bson (>= 1.3.1)
mongoid (2.1.7)
activemodel (~> 3.0)
mongo (~> 1.3)
tzinfo (~> 0.3.22)
mongoid-rspec (1.4.4)
mongoid (~> 2.0)
rspec (~> 2)
nokogiri (1.5.0)
polyglot (0.3.2)
rack (1.2.3)
rack-mount (0.6.14)
rack (>= 1.0.0)
rack-test (0.5.7)
rack (>= 1.0)
rails (3.0.9)
actionmailer (= 3.0.9)
actionpack (= 3.0.9)
activerecord (= 3.0.9)
activeresource (= 3.0.9)
activesupport (= 3.0.9)
bundler (~> 1.0)
railties (= 3.0.9)
railties (3.0.9)
actionpack (= 3.0.9)
activesupport (= 3.0.9)
rake (>= 0.8.7)
rdoc (~> 3.4)
thor (~> 0.14.4)
rake (0.9.2)
rb-fsevent (0.4.3)
rdoc (3.9.2)
rspec (2.6.0)
rspec-core (~> 2.6.0)
rspec-expectations (~> 2.6.0)
rspec-mocks (~> 2.6.0)
rspec-core (2.6.4)
rspec-expectations (2.6.0)
diff-lcs (~> 1.1.2)
rspec-mocks (2.6.0)
rspec-rails (2.6.1)
actionpack (~> 3.0)
activesupport (~> 3.0)
railties (~> 3.0)
rspec (~> 2.6.0)
rubyzip (0.9.4)
selenium-webdriver (2.4.0)
childprocess (>= 0.2.1)
ffi (>= 1.0.7)
json_pure
rubyzip
term-ansicolor (1.0.6)
thor (0.14.6)
treetop (1.4.10)
polyglot
polyglot (>= 0.3.1)
tzinfo (0.3.29)
xpath (0.1.4)
nokogiri (~> 1.3)

PLATFORMS
ruby

DEPENDENCIES
bcrypt-ruby
bson_ext (~> 1.3)
cucumber-rails
factory_girl_rails
guard
guard-rspec
mail
mongoid (~> 2.1)
mongoid-rspec
rails (= 3.0.9)
rb-fsevent
rspec-rails

Durran Jordan

unread,
Aug 13, 2011, 9:35:18 AM8/13/11
to mon...@googlegroups.com
And what does your user model look like?

2011/8/13 midnightcowboy <matthia...@gmail.com>

midnightcowboy

unread,
Aug 15, 2011, 4:25:38 AM8/15/11
to Mongoid
I think I've tracked down the problem, but I'm still not sure whether
it's a problem with Mongoid, MongoDB, Rails or simply my app.

Basically I have a field 'identifier' in each of my models which is
used as key, and the field is populated automatically when creating a
new record. This has always worked without problems, until I updated
my gems as mentioned in the original post.

All my models look similar to this:

class User
include Mongoid::Document
include Mongoid::Timestamps
include GenerateIdentifier

field :identifier

(…)

key :identifier
end

Contrary to my original post, data is still returned as objects now.
For some strange reason though I can access all fields in object style
except for the identifier field. The example in the original post was
simplified and as it turns out not correct. Here's an actual example:

ruby-1.9.2-p136 :001 > user = User.where(:email =>
"te...@example.com").first
=> #<User _id: 4e445f23058f8e703a7c5e12, _type: nil, identifier:
"b2b3f068a7755a5", first_name: "Matt", last_name: "Test", email:
"te...@example.com", (…) >
ruby-1.9.2-p136 :002 > user.identifier
=> nil
ruby-1.9.2-p136 :003 > user['identifier']
=> "b2b3f068a7755a5"
ruby-1.9.2-p136 :004 > user.first_name
=> "Matt"
ruby-1.9.2-p136 :005 > user.last_name
=> "Test"

Could it be that 'identifier' is now a reserved word in Mongoid? It
doesn't seem to be a reserved word in Rails.

Nick Hoffman

unread,
Aug 15, 2011, 8:54:13 AM8/15/11
to mon...@googlegroups.com
Ah, there's your problem: "identifier" is a reserved word. It shouldn't be the name of a field. See Mongoid.destructive_fields for a list of reserved words:
https://github.com/mongoid/mongoid/blob/master/lib/mongoid/config.rb#L99-113

There's an issue open for this in GitHub. Are you Aanand?
https://github.com/mongoid/mongoid/issues/1146

midnightcowboy

unread,
Aug 15, 2011, 9:10:34 AM8/15/11
to Mongoid
Thank you! I'm glad I finally know what the problem is. Hm I guess I
should check the issues on Github next time, might help :)
And no, I'm not Aanand.
Reply all
Reply to author
Forward
0 new messages