Skip to content

Instantly share code, notes, and snippets.

@MichaelXavier
Created April 11, 2010 00:55
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 MichaelXavier/362410 to your computer and use it in GitHub Desktop.
Save MichaelXavier/362410 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
#
# A one file test to show ...
require 'rubygems'
require 'dm-core'
# setup the logger
DataMapper::Logger.new(STDOUT, :debug)
# connect to the DB
DataMapper.setup(:default, 'sqlite3::memory:')
class User
include DataMapper::Resource
# properties
property :id, Serial
has n, :children
has n, :grandchildren, :through => :children
end
class Child
include DataMapper::Resource
# properties
property :id, Serial
belongs_to :user
has n, :grandchildren
end
class Grandchild
include DataMapper::Resource
# properties
property :id, Serial
belongs_to :child
belongs_to :user, :through => :child
has n, :greatgrandchildren
end
class Greatgrandchild
include DataMapper::Resource
# properties
property :id, Serial
belongs_to :grandchild
belongs_to :user, :through => :grandchild
end
DataMapper.auto_migrate!
# put the code here!
@user1 = User.create
@child = @user1.children.create
@grandchild = @child.grandchildren.create
@greatgrandchild = @grandchild.greatgrandchildren.create
@user2 = User.create
@child = @user2.children.create
@grandchild = @child.grandchildren.create
@greatgrandchild = @grandchild.greatgrandchildren.create
@user = User.first
p Greatgrandchild.all(:user => @user)
__END__
#SQL output includes this:
SELECT "id", "grandchild_id" FROM "greatgrandchildren" WHERE "id" = 1 ORDER BY "id"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment