Skip to content

Instantly share code, notes, and snippets.

@yannick
Created June 7, 2009 08:20
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 yannick/125240 to your computer and use it in GitHub Desktop.
Save yannick/125240 to your computer and use it in GitHub Desktop.
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