Skip to content

Instantly share code, notes, and snippets.

@hammerdr
Created December 4, 2011 18:24
Show Gist options
  • Save hammerdr/1430907 to your computer and use it in GitHub Desktop.
Save hammerdr/1430907 to your computer and use it in GitHub Desktop.
class User
include Mongoid::Document
field :name
end
users = User.all.cache
users.each { |u| puts u } # Database hit
users.each { |u| puts u } # No database hit
class Post
include Mongoid::Document
cache
field :name
embeds_many :comments
end
posts = Post.all
posts.each { |p| puts p } # Database hit
posts.each { |p| puts p } # No database hit (even for the comments)
class Doctor
include Mongoid::Document
cache
references_many :patients
end
doctors = Doctor.all
doctors.each { |d| puts d } # Database hit
doctors.each { |d| puts d } # Not really sure? Is this a database hit for the references?
def users
User.all.cache
end
def index
if users.count > 5 # Database hit
render_for(users) # Database hit because we're defining a new
criteria
else
...
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment