Created
October 2, 2010 18:29
-
-
Save bkeepers/607865 to your computer and use it in GitHub Desktop.
Idea for lazily loading attributes in MM
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>> 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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