Skip to content

Instantly share code, notes, and snippets.

@stellard
Created January 4, 2011 10:43
Show Gist options
  • Save stellard/764632 to your computer and use it in GitHub Desktop.
Save stellard/764632 to your computer and use it in GitHub Desktop.
This test shows how you cannot search on an embedded document's hash
require "test/unit"
require "rubygems"
require "mongoid"
Mongoid.configure do |config|
name = "embed_hash_test"
host = "localhost"
config.master = Mongo::Connection.new.db(name)
config.persist_in_safe_mode = false
end
class RootDoc
include Mongoid::Document
field :data, :type => Hash
embeds_many :embedded_docs
end
class EmbeddedDoc
include Mongoid::Document
field :data, :type => Hash
embedded_in :root_doc, :inverse_of => :embedded_docs
end
RootDoc.delete_all
class EmbeddedHashSearch < Test::Unit::TestCase
#passing test
def test_root_doc_hash_search
doc = RootDoc.create :data => {"test" => "searching on a root doc" }
assert_equal RootDoc.where("data.test" => "searching on a root doc" ).first, doc
end
#failing test
def test_embedded_hash_search
root = RootDoc.new
embed2 = EmbeddedDoc.new :data => {"test" => "should not find this one" }
root.embedded_docs << embed2
embed2.save
embed = EmbeddedDoc.new :data => {"test" => "searching on embedded association" }
root.embedded_docs << embed
embed.save
assert_equal root.embedded_docs.where("data.test" => "searching on embedded association" ).to_a, [embed]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment