Skip to content

Instantly share code, notes, and snippets.

Created November 1, 2009 13:56
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save mislav/223533 to your computer and use it in GitHub Desktop.
DataMapper self-referential many-many
require 'dm-core'$stdout, :debug)
DataMapper.setup(:default, 'sqlite3::memory:')
class User
include DataMapper::Resource
has n, :follows
has n, :followings, :model => 'Follow', :child_key => [:target_id]
# can't get it to work
has n, :targets, :through => :follows, :model => 'User'
class Follow
include DataMapper::Resource
belongs_to :user
belongs_to :target, :model => 'User'
>> user = User.create
>> user.targets
~ SELECT "users"."id", "users"."screen_name", "users"."full_name", "users"."created_at", "users"."updated_at", "users"."avatar_url", "users"."followers", "users"."following", "users"."tweets" FROM "users"
INNER JOIN "follows" ON "users"."id" = "follows"."target_id"
INNER JOIN "users" ON "follows"."user_id" = "users"."id"
WHERE "follows"."user_id" = 1
GROUP BY "users"."id", "users"."screen_name", "users"."full_name", "users"."created_at", "users"."updated_at", "users"."avatar_url", "users"."followers", "users"."following", "users"."tweets" ORDER BY "users"."id"
ERROR: ambiguous column name
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment