Created
June 7, 2009 08:20
-
-
Save yannick/125240 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
require 'rubygems' | |
require 'dm-core' | |
require 'spec' | |
require 'dm_redis_adapter' | |
require 'dm-validations' | |
DataMapper::Logger.new(STDOUT, :debug) | |
DataMapper.setup(:default, {:adapter => "redis"}) | |
class User | |
include DataMapper::Resource | |
validates_is_unique :name | |
property :id, Serial | |
property :name, String, :unique => true | |
has n, :friendships | |
has n, :friends, :through => :friendships, :model => self # XXX: model should not be necessary to specify, since it should use whatever the target_model for is Frienship.friends | |
end | |
class Friendship | |
include DataMapper::Resource | |
property :user_id, Integer, :key => true | |
property :friend_id, Integer, :key => true | |
belongs_to :user | |
belongs_to :friend, :model => User | |
end | |
@ted = User.create(:name => 'Ted') | |
@dan = User.create(:name => 'Dan') | |
User::Friendship.create(:user => @ted, :friend => @dan) | |
@ted.friendships | |
@ted.friends | |
@dan.friendships | |
@dan.friends | |
User.all #works | |
@ted = User.create(:name => 'Ted') #should not work, but does | |
User.all #creates error then | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment