Created
June 6, 2009 23:56
-
-
Save knowtheory/125061 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 'dm-core' | |
| require 'spec' | |
| DataMapper::Logger.new(STDOUT, :debug) | |
| DataMapper.setup(:default, "mysql://localhost/dm_friends") | |
| class User | |
| include DataMapper::Resource | |
| property :id, Serial | |
| property :name, String | |
| has n, :friendships, :model => "User::Friendship" | |
| has n, :friends, :through => :friendships, :model => "User", :child_key => [:friend_id] | |
| class Friendship | |
| include DataMapper::Resource | |
| property :user_id, Integer, :key => true | |
| property :friend_id, Integer, :key => true | |
| belongs_to :user, :child_key => [:user_id] | |
| belongs_to :friend, :model => "User", :child_key => [:friend_id] | |
| end | |
| end | |
| DataMapper.auto_migrate! | |
| describe User do | |
| it "can have friends" do | |
| @ted = User.create(:name=> "Ted") | |
| @dan = User.create(:name=> "Dan") | |
| User::Friendship.create(:user_id => @ted.id, :friend_id => @dan.id) | |
| @ted.friendships.should_not be_empty | |
| @ted.friends.should_not be_empty | |
| end | |
| end |
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
| umbra:dm-specs ted$ irb | |
| >> require 'dm_self_referential_friends' | |
| ~ (0.000084) SET sql_auto_is_null = 0 | |
| ~ (0.000073) SET SESSION sql_mode = 'ANSI,NO_AUTO_VALUE_ON_ZERO,NO_DIR_IN_CREATE,NO_ENGINE_SUBSTITUTION,NO_UNSIGNED_SUBTRACTION,TRADITIONAL' | |
| ~ (0.002173) DROP TABLE IF EXISTS "user_friendships" | |
| ~ (0.001259) DROP TABLE IF EXISTS "users" | |
| ~ (0.000443) SHOW TABLES LIKE 'user_friendships' | |
| ~ (0.000066) SET sql_auto_is_null = 0 | |
| ~ (0.000072) SET SESSION sql_mode = 'ANSI,NO_AUTO_VALUE_ON_ZERO,NO_DIR_IN_CREATE,NO_ENGINE_SUBSTITUTION,NO_UNSIGNED_SUBTRACTION,TRADITIONAL' | |
| ~ (0.000703) SHOW VARIABLES LIKE 'character_set_connection' | |
| ~ (0.000607) SHOW VARIABLES LIKE 'collation_connection' | |
| ~ (0.342712) CREATE TABLE "user_friendships" ("user_id" INTEGER NOT NULL, "friend_id" INTEGER NOT NULL, PRIMARY KEY("user_id", "friend_id")) ENGINE = InnoDB CHARACTER SET utf8 COLLATE utf8_general_ci | |
| ~ (0.000361) SHOW TABLES LIKE 'users' | |
| ~ (0.098797) CREATE TABLE "users" ("id" INTEGER NOT NULL AUTO_INCREMENT, "name" VARCHAR(50), PRIMARY KEY("id")) ENGINE = InnoDB CHARACTER SET utf8 COLLATE utf8_general_ci | |
| => true | |
| >> @ted = User.create(:name=> "Ted") | |
| ~ (0.032618) INSERT INTO "users" ("name") VALUES ('Ted') | |
| => #<User @id=1 @name="Ted"> | |
| >> @dan = User.create(:name=> "Dan") | |
| ~ (0.001171) INSERT INTO "users" ("name") VALUES ('Dan') | |
| => #<User @id=2 @name="Dan"> | |
| >> User::Friendship.create(:user_id => @ted.id, :friend_id => @dan.id) | |
| ~ (0.001395) INSERT INTO "user_friendships" ("user_id", "friend_id") VALUES (1, 2) | |
| => #<User::Friendship @user_id=1 @friend_id=2> | |
| >> @ted.friendships | |
| ~ (0.000275) SELECT "user_id", "friend_id" FROM "user_friendships" WHERE "user_id" = 1 | |
| => [#<User::Friendship @user_id=1 @friend_id=2>] | |
| >> @ted.friends | |
| NoMethodError: undefined method `name' for nil:NilClass | |
| from /Library/Ruby/Gems/1.8/gems/dm-core-0.10.0/lib/dm-core/property_set.rb:150:in `initialize' | |
| from /Library/Ruby/Gems/1.8/gems/dm-core-0.10.0/lib/dm-core/property_set.rb:150:in `map' | |
| from /Library/Ruby/Gems/1.8/gems/dm-core-0.10.0/lib/dm-core/property_set.rb:150:in `initialize' | |
| from /Library/Ruby/Gems/1.8/gems/dm-core-0.10.0/lib/dm-core/associations/many_to_many.rb:23:in `new' | |
| from /Library/Ruby/Gems/1.8/gems/dm-core-0.10.0/lib/dm-core/associations/many_to_many.rb:23:in `target_key' | |
| from /Library/Ruby/Gems/1.8/gems/dm-core-0.10.0/lib/dm-core/associations/relationship.rb:161:in `query_for' | |
| from /Library/Ruby/Gems/1.8/gems/dm-core-0.10.0/lib/dm-core/associations/one_to_many.rb:42:in `collection_for' | |
| from /Library/Ruby/Gems/1.8/gems/dm-core-0.10.0/lib/dm-core/associations/one_to_many.rb:131:in `lazy_load' | |
| from /Library/Ruby/Gems/1.8/gems/dm-core-0.10.0/lib/dm-core/associations/one_to_many.rb:61:in `get' | |
| from /Library/Ruby/Gems/1.8/gems/dm-core-0.10.0/lib/dm-core/associations/one_to_many.rb:96:in `friends' | |
| from (irb):6 | |
| >> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment