Skip to content

Instantly share code, notes, and snippets.

@xaviershay
Created January 11, 2011 04:29
Show Gist options
  • Select an option

  • Save xaviershay/774018 to your computer and use it in GitHub Desktop.

Select an option

Save xaviershay/774018 to your computer and use it in GitHub Desktop.
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