Skip to content

Instantly share code, notes, and snippets.

@bkeepers
Created October 2, 2010 18:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bkeepers/607865 to your computer and use it in GitHub Desktop.
Save bkeepers/607865 to your computer and use it in GitHub Desktop.
Idea for lazily loading attributes in MM
>> user = User..first
=> …
>> user.collaborators_loaded?
=> false
>> user.collaborators.size # <= loads all lazy fields
=> 30
>> user.collaborators_loaded?
=> true
>> user.time_zone_loaded?
=> true
# Using #fields overrides lazy settings
>> user = User.fields(:name).first
=> …
>> user.email_loaded?
=> false
>> user.email # <= loads lazy fields
=> "brandon@example.com"
>> user.email_loaded?
=> true
>> user.collaborators_loaded?
=> true
class User
include MongoMapper::Document
key :name
key :email
key :time_zone, :lazy => true
many :collaborators, :lazy => true
end
class Collaborator
include MongoMapper::EmbeddedDocument
timestamps!
belongs_to :user
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment