Question

I have this document call it Blog, Post, with embeds_many :posts, which by itself embeds_many :comments. It was giving me error "Can't convert String into Integer" when I try to save some blogs. Upon investigation I found embedded post documents with empty data, all fields nil, and those post objects would give the same error "can't covert String into Integer" error.

Why it was nil might be something from my code, but the problem is I can't save, update, nor destroy this post or do anything with the comments, so I'm stuck. I have to manually login into the mongodb console and delete those objects.

Any idea why this happens and how to handle it?

If it's of any relation, I am using MongoHQ.

Was it helpful?

Solution

I'm facing a similar problem. It does not concern mongoDB service at all.

To clarify

class A
  include Mongoid::Document
  embeds_many :bs, class_name: 'B'
end

class B
  include Mongoid::Document
  embedded_in :a, class_name: 'A'
  embeds_many :cs, class_name: 'C'
end

class C
  include Mongoid::Document
  embedded_in :b, class_name: 'B'
end

In my case, if I comment B relations to C, it works perfectly But I soon I connect B and C, I get the following stack trace:

TypeError - can't convert String into Integer: () Users/my_user/.rvm/gems/ruby-1.9.3-p194/bundler/gems/mongoid-018e40e11a31/lib/mongoid/relations/proxy.rb:149:in `[]' () Users/my_user/.rvm/gems/ruby-1.9.3-p194/bundler/gems/mongoid-018e40e11a31/lib/mongoid/relations/proxy.rb:149:in `method_missing' () Users/my_user/.rvm/gems/ruby-1.9.3-p194/bundler/gems/mongoid-018e40e11a31/lib/mongoid/relations/embedded/many.rb:402:in `method_missing' () Users/my_user/.rvm/gems/ruby-1.9.3-p194/bundler/gems/mongoid-018e40e11a31/lib/mongoid/fields.rb:75:in `apply_default' () Users/my_user/.rvm/gems/ruby-1.9.3-p194/bundler/gems/mongoid-018e40e11a31/lib/mongoid/fields.rb:47:in `block in apply_pre_processed_defaults' () Users/my_user/.rvm/gems/ruby-1.9.3-p194/bundler/gems/mongoid-018e40e11a31/lib/mongoid/fields.rb:46:in `each' () Users/my_user/.rvm/gems/ruby-1.9.3-p194/bundler/gems/mongoid-018e40e11a31/lib/mongoid/fields.rb:46:in `apply_pre_processed_defaults' () Users/my_user/.rvm/gems/ruby-1.9.3-p194/bundler/gems/mongoid-018e40e11a31/lib/mongoid/document.rb:110:in `block in initialize' () Users/my_user/.rvm/gems/ruby-1.9.3-p194/bundler/gems/mongoid-018e40e11a31/lib/mongoid/threaded/lifecycle.rb:84:in `_building' () Users/my_user/.rvm/gems/ruby-1.9.3-p194/bundler/gems/mongoid-018e40e11a31/lib/mongoid/document.rb:106:in `initialize'

I'm gonna try to inspect relations via #reflect_on_association to see if mongoid may get lost

---- EDIT && own solution ----

Exploring the stack trace with the debbuger, it seams at some point mongoid call the _id method and cannot find any proper method response to it.

My intuition was that something broke the model initialisation during relation resolution.

So you have to triple check relation definition.

In my case the solution came by magic.

embeds_many :fields
#that I replaced by
embeds_many :resource_fields

And not it works properly with the class_name definition I show above. Why ? because I guess fields is a mongoid method used during the initialisation.

Hope it help you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top