Permalink
Browse files

Add mongoid example file

  • Loading branch information...
1 parent d35eaab commit ec65b665430a13eab135fcd22b3b766bb8e8c8ea @arthurnn arthurnn committed Nov 24, 2013
Showing with 31 additions and 0 deletions.
  1. +31 −0 examples/mongoid_test.rb
View
@@ -0,0 +1,31 @@
+require 'mongoid'
+require 'mongoid/support/query_counter'
+
+Mongoid.configure.connect_to("mongoid_test")
+
+def count_queries(&block)
+ query_counter = Mongoid::QueryCounter.new
+ query_counter.instrument(&block)
+ query_counter.events.size
+end
+
+class Post
+ include Mongoid::Document
+ belongs_to :person
+end
+
+class Person
+ include Mongoid::Document
+ has_many :posts
+ field :name
+end
+
+p = Person.create!(name: 'arthurnn')
+post1 = Post.create!(person: p)
+post2 = Post.create!(person: p)
+
+query_counter = count_queries do
+ Person.includes(:posts).all.to_a
+end
+
+p query_counter

0 comments on commit ec65b66

Please sign in to comment.