1

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.

Bashar Abdullah
  • 1,545
  • 1
  • 16
  • 27

1 Answers1

2

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.

ProxyGear
  • 835
  • 1
  • 10
  • 26
  • Glad it's working for you and thanks for sharing it. But in my case, I'm not using what seems to be a mongoid reserved method. Could probably have similarity, but essentially different I think. – Bashar Abdullah Nov 20 '13 at 18:45
  • Actually the problem just popup again for me. I get the same error but with a different stack trace: /....../.rvm/gems/ruby-1.9.3-p194/bundler/gems/mongoid-8187a8073dfc/lib/mongoid/attributes.rb:297:in `[]' – ProxyGear Nov 22 '13 at 13:37
  • @BasharAbdullah Can you provide your stack trace ? – ProxyGear Nov 22 '13 at 14:18
  • I will see if the problem still happens, and get you s track trace then. I kinda ignored it for long time so not sure if I can regenerate. – Bashar Abdullah Nov 22 '13 at 15:33