Created
April 11, 2010 00:55
-
-
Save MichaelXavier/362410 to your computer and use it in GitHub Desktop.
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
#!/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