title: DataMapper's many-to-many get all foreign IDs
body: <p><strong>NEW QUESTION:</strong></p>
  
  <p>easier test case, create a database</p>
  
  <pre><code>CREATE USER test WITH PASSWORD $$test$$;
  CREATE DATABASE test;
  GRANT ALL PRIVILEGES ON DATABASE test TO test;
  # add to pg_hba.conf if needed
  </code></pre>
  
  <p>datamapper setup:</p>
  
  <pre><code>require 'data_mapper'
  DataMapper::Logger.new($stdout, :debug)
  DataMapper.setup(:default, 'postgres://test:test@127.0.0.1/test')
  
  class Membership
    include DataMapper::Resource
    property :id, Serial
    property :ended_at, DateTime
  
    belongs_to :user, key: true
    belongs_to :group, key: true
  end
  
  class Group
    include DataMapper::Resource
    property :id, Serial
    property :name, String
  
    has n, :memberships
    has n, :users, through: :memberships
  end
  
  class User
    include DataMapper::Resource
    property :id, Serial
    property :name, String
  
    has n, :memberships
    has n, :groups, through: :memberships
  end
  
  DataMapper.finalize
  DataMapper.auto_migrate!
  
  u1 = User.create name: 'u1'
  u2 = User.create name: 'u2'
  g1 = Group.create name: 'g1'
  g2 = Group.create name: 'g2'
  g3 = Group.create name: 'g3'
  
  u1.groups &lt;&lt; g1
  u1.groups &lt;&lt; g3
  u2.groups &lt;&lt; g2
  g3.users &lt;&lt; u2
  u1.save
  u2.save
  g3.save
  
  # User.all = [#&lt;User @id=1 @name="u1"&gt;, #&lt;User @id=2 @name="u2"&gt;
  # Group.all = [#&lt;Group @id=1 @name="g1"&gt;, #&lt;Group @id=2 @name="g2"&gt;, #&lt;Group @id=3 @name="g3"&gt;]
  # Membership.all = [#&lt;Membership @id=1 @ended_at=nil @user_id=2 @group_id=2&gt;, #&lt;Membership @id=2 @ended_at=nil @user_id=2 @group_id=3&gt;, #&lt;Membership @id=3 @ended_at=nil @user_id=1 @group_id=1&gt;, #&lt;Membership @id=4 @ended_at=nil @user_id=1 @group_id=3&gt;] 
  </code></pre>
  
  <p>how to get all ids of User from all some group, and all ids from Groups from some users?</p>
  
  <pre><code>user_ids = [1,2]
  User.all(id:user_ids).what???
  # desired output: {'1' =&gt; [1,3], '2' =&gt; [2,3]}   
  #   user with ID=1, has two groups (with ID=1 and ID=3)
  #   user with ID=2, also has two groups (with ID=2 and ID=3)
  
  group_ids = [1,3]
  Group.all(id:group_ids).what???
  # desired output: {'1' =&gt; [1], '3' =&gt; [2,3]} 
  #   group with ID=1, has one user (with ID=1)
  #   group with ID=3, has two users (with ID=2 and ID=3)
  </code></pre>
  
  <p><strong>OLD QUESTION:</strong></p>
  
  <p>for example, if i have 3 tables</p>
  
  <pre><code>class Human
    include DataMapper::Resource
    property :id, Serial 
    property :name, String
    has n, :memberships
  end
  
  class Group
    include DataMapper::Resource
    property :id, Serial 
    property :name, String
    has n, :memberships
  end
  
  class Membership
    include DataMapper::Resource
    property :ended_at, DateTime
    belongs_to :human, key: true
    belongs_to :group, key: true
  end
  </code></pre>
  
  <p>how to get all Group.id from a Human? or Human.id from a Group? so it would be shown like this:</p>
  
  <pre><code>human_ids = [1,3,4]
  group_ids = Human.get_all_group_ids??? human_ids
  group_ids # { '1' =&gt; [3,5,7], '3' =&gt; [1,9], '4' =&gt; [6,7,9,12] }
  </code></pre>
  
  <p>is it possible to do this in simple way on DataMapper? with the most efficient query as possible? </p>
  
  <p>or should i do manually?</p>
  
  <pre><code>sql = "
   SELECT x1.id AS hid, x3.id AS gid
   FROM humans x1
     LEFT JOIN memberships x2
       ON x1.id = x2.human_id
     LEFT JOIN groups x3
       ON x2.group_id = x3.id
   WHERE x1.id IN ( #{human_ids.join ','} )
   ORDER BY 1,2
  "
  
  rows = repository(:default).adapter.select sql
  
  res = {}
  rows.each do |row|
    hid = row[:hid]
    res[hid] = [] if res[hid].nil?
    res[hid] &lt;&lt; row[:gid]
  end
  
  # res is the result
  </code></pre>