-
Type: Task
-
Resolution: Done
-
Affects Version/s: None
-
Component/s: None
It is possible I am not using this correctly, but I haven't been able to find much documentation about it's use. Using Mongoid 2.3.4 I'm finding that even though I do eager loading on a has_one relation, the database is still getting hit when I try to access the relation. Here are my models:
class BookShelf include Mongoid::Document include Mongoid::Timestamps has_one :book end class Book include Mongoid::Document include Mongoid::Timestamps field :name, :default => "Test" belongs_to :book_shelf end
And here's my test case:
ruby-1.9.2-p180 :003 > Mongoid.identity_map_enabled => true ruby-1.9.2-p180 :004 > @bookshelves = BookShelf.includes(:book).all.to_a => [#<BookShelf _id: 4ed7b86d1dbbbb700c000001, _type: nil, created_at: 2011-12-01 17:25:01 UTC, updated_at: 2011-12-01 17:25:01 UTC>, #<BookShelf _id: 4ed7b86d1dbbbb700c000002, _type: nil, created_at: 2011-12-01 17:25:01 UTC, updated_at: 2011-12-01 17:25:01 UTC>, #<BookShelf _id: 4ed7b86d1dbbbb700c000003, _type: nil, created_at: 2011-12-01 17:25:01 UTC, updated_at: 2011-12-01 17:25:01 UTC>, #<BookShelf _id: 4ed7b86d1dbbbb700c000004, _type: nil, created_at: 2011-12-01 17:25:01 UTC, updated_at: 2011-12-01 17:25:01 UTC>, #<BookShelf _id: 4ed7b86d1dbbbb700c000005, _type: nil, created_at: 2011-12-01 17:25:01 UTC, updated_at: 2011-12-01 17:25:01 UTC>, #<BookShelf _id: 4ed7b86d1dbbbb700c000006, _type: nil, created_at: 2011-12-01 17:25:01 UTC, updated_at: 2011-12-01 17:25:01 UTC>, #<BookShelf _id: 4ed7b86d1dbbbb700c000007, _type: nil, created_at: 2011-12-01 17:25:01 UTC, updated_at: 2011-12-01 17:25:01 UTC>, #<BookShelf _id: 4ed7b86d1dbbbb700c000008, _type: nil, created_at: 2011-12-01 17:25:01 UTC, updated_at: 2011-12-01 17:25:01 UTC>, #<BookShelf _id: 4ed7b86d1dbbbb700c000009, _type: nil, created_at: 2011-12-01 17:25:01 UTC, updated_at: 2011-12-01 17:25:01 UTC>, #<BookShelf _id: 4ed7b86d1dbbbb700c00000a, _type: nil, created_at: 2011-12-01 17:25:01 UTC, updated_at: 2011-12-01 17:25:01 UTC>] ruby-1.9.2-p180 :007 > Mongoid::IdentityMap.get(Book, "book_shelf_id" => @bookshelves.first.id) => #<Book _id: 4ed7ba5d1dbbbb70cc000002, _type: nil, created_at: 2011-12-01 17:33:17 UTC, updated_at: 2011-12-01 17:33:17 UTC, name: "Test", book_shelf_id: BSON::ObjectId('4ed7b86d1dbbbb700c000001')> ruby-1.9.2-p180 :008 > @bookshelves.first.book => #<Book _id: 4ed7ba5d1dbbbb70cc000002, _type: nil, created_at: 2011-12-01 17:33:17 UTC, updated_at: 2011-12-01 17:33:17 UTC, name: "Test", book_shelf_id: BSON::ObjectId('4ed7b86d1dbbbb700c000001')>
When I execute the last call, I still see a call to the database:
Thu Dec 1 11:24:39 [conn37] query: mongotest_development.books{ $query: { book_shelf_id: ObjectId('4ed7b86d1dbbbb700c000001') }, $orderby: { _id: 1 } }
Thu Dec 1 11:24:39 [conn37] running multiple plans
Thu Dec 1 11:24:39 [conn37] used cursor: 0x100e31e20
Thu Dec 1 11:24:39 [conn37] query mongotest_development.books ntoreturn:1 reslen:140 nreturned:1 0ms
I would have expected that with eager loading and the book in the identity map that this call to the database should not occur.