Created
January 11, 2011 04:29
-
-
Save xaviershay/774018 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| require 'rubygems' | |
| require 'dm-core' | |
| require 'dm-migrations' | |
| DataMapper::Logger.new($stdout, :debug) | |
| DataMapper.setup(:default, 'postgres://localhost/test') # createdb test | |
| class Group | |
| include DataMapper::Resource | |
| property :id, Serial | |
| has n, :users | |
| end | |
| class User | |
| include DataMapper::Resource | |
| property :id, Serial | |
| belongs_to :group | |
| has n, :posts | |
| end | |
| class Post | |
| include DataMapper::Resource | |
| property :id, Serial | |
| property :title, String | |
| belongs_to :user | |
| end | |
| DataMapper.finalize | |
| DataMapper.auto_migrate! | |
| group = Group.create( | |
| :users => [ | |
| {:posts => [{:title => "Post A"}]}, | |
| {:posts => [{:title => "Post B"}]} | |
| ]) | |
| group.reload | |
| group.users.to_a.each do |user| # The .to_a cannot be avoided | |
| user.posts.each do |post| # How to eager load these posts? | |
| puts post.title | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment