Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
DataMapper self-referential many-many
require 'dm-core'
DataMapper::Logger.new($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'
end
class Follow
include DataMapper::Resource
belongs_to :user
belongs_to :target, :model => 'User'
end
DataMapper.auto_migrate!
>> 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