Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fix issues with Mongoid gem (incorrect JSON serialization of Moped::B…

…SON::ObjectId)

To fix issues with the Mongoid gem, call `document.id.as_json` in the
`Tire::Index#get_id_from_document` method.

Note: Still not being able to replicate the problem locally with a Mongoid Rails application.

Related: #529, #535
  • Loading branch information...
commit 297683f9405aae93f8fd1cdc0ee8454e0148f14b 1 parent d80b262
@karmi authored
Showing with 27 additions and 1 deletion.
  1. +1 −1  lib/tire/index.rb
  2. +26 −0 test/unit/index_test.rb
View
2  lib/tire/index.rb
@@ -415,7 +415,7 @@ def get_id_from_document(document)
when document.is_a?(Hash)
document[:_id] || document['_id'] || document[:id] || document['id']
when document.respond_to?(:id) && document.id != document.object_id
- document.id
+ document.id.as_json
end
$VERBOSE = old_verbose
id
View
26 test/unit/index_test.rb
@@ -304,6 +304,32 @@ class MyDocument;end; document = MyDocument.new
@index.store Article.new(:id => 123, :title => 'Test', :body => 'Lorem')
end
+ should "convert document ID to string or number" do
+ class Document1
+ def id
+ "one"
+ end
+ end
+ class Document2
+ def id
+ 1
+ end
+ end
+ class Document3
+ class ID; def as_json; 'c'; end; end
+ def id
+ ID.new
+ end
+ end
+ document_1 = Document1.new
+ document_2 = Document2.new
+ document_3 = Document3.new
+
+ assert_equal 'one', @index.get_id_from_document(document_1)
+ assert_equal 1, @index.get_id_from_document(document_2)
+ assert_equal 'c', @index.get_id_from_document(document_3)
+ end
+
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.