title: Active record multiple joins returning wrong values and duplicates Rails
body: <p>I've got below query on my custom class in Rails 4:</p>
  
  <pre><code>User.joins(:roles).joins(:events).joins(:booths).group("users.id, roles.id")
  </code></pre>
  
  <p>This however returns multiple duplicates of wrong users.</p>
  
  <p>I want to be able to return User, user's roles, events and booths</p>
  
  <p>Below is the user model associations:</p>
  
  <pre><code>  after_create :assign_default_role
    rolify :before_add =&gt; :before_add_method
    # Include default devise modules. Others available are:
    # :confirmable, :lockable, :timeoutable and :omniauthable
    devise :database_authenticatable, :registerable,
           :recoverable, :rememberable, :trackable, :validatable,
           :confirmable, :omniauthable, :lastseenable, :invitable
    devise :timeoutable, :timeout_in =&gt; 2.weeks
  
    has_and_belongs_to_many :events, :autosave =&gt; true
  
    has_many :venues
    has_many :booths
    has_many :webcasts
    # has_many :events, through: :venues
    has_many :from_user_chats, :foreign_key =&gt; 'from_user_id', :class_name =&gt; 'Chat'
    has_many :to_user_chats, :foreign_key =&gt; 'to_user_id', :class_name =&gt; 'Chat'
    has_one :uploaded_file, as: :imageable, dependent: :destroy
    accepts_nested_attributes_for :uploaded_file, :reject_if =&gt; proc { |attributes| attributes['assets'].blank? }
  </code></pre>
  
  <p>Is there anything that I'm missing here?</p>
  
  <p>What do I need to do to get all users with those additional information from those other tables?</p>
  
  <p><strong>Output of <code>explain</code></strong></p>
  
  <pre><code>=&gt; EXPLAIN for: SELECT "users".* FROM "users" INNER JOIN "users_roles" ON "users_roles"."user_id" = "users"."id" INNER JOIN "roles" ON "roles"."id" = "users_roles"."role_id" INNER JOIN "events_users" ON "events_users"."user_id" = "users"."id" INNER JOIN "events" ON "events"."id" = "events_users"."event_id" INNER JOIN "booths" ON "booths"."user_id" = "users"."id" GROUP BY users.id, roles.id
                                                                   QUERY PLAN
  ---------------------------------------------------------------------------------------------------------------------------------------------
   HashAggregate  (cost=6.65..6.66 rows=1 width=3538)
     -&gt;  Nested Loop  (cost=6.01..6.65 rows=1 width=3538)
           -&gt;  Merge Join  (cost=5.86..6.47 rows=1 width=3538)
                 Merge Cond: (users_roles.user_id = users.id)
                 -&gt;  Nested Loop  (cost=4.78..281.23 rows=963 width=16)
                       -&gt;  Nested Loop  (cost=4.49..232.77 rows=54 width=8)
                             Join Filter: (events_users.event_id = events.id)
                             -&gt;  Nested Loop  (cost=4.49..227.48 rows=54 width=12)
                                   -&gt;  Index Only Scan using index_booths_on_user_id on booths  (cost=0.13..16.22 rows=6 width=4)
                                   -&gt;  Bitmap Heap Scan on events_users  (cost=4.36..35.12 rows=9 width=8)
                                         Recheck Cond: (user_id = booths.user_id)
                                         -&gt;  Bitmap Index Scan on index_events_users_on_user_id_and_event_id  (cost=0.00..4.36 rows=9 width=0)
                                               Index Cond: (user_id = booths.user_id)
                             -&gt;  Materialize  (cost=0.00..2.06 rows=4 width=4)
                                   -&gt;  Seq Scan on events  (cost=0.00..2.04 rows=4 width=4)
                       -&gt;  Index Only Scan using index_users_roles_on_user_id_and_role_id on users_roles  (cost=0.29..0.72 rows=18 width=8)
                             Index Cond: (user_id = events_users.user_id)
                 -&gt;  Sort  (cost=1.08..1.09 rows=4 width=3534)
                       Sort Key: users.id
                       -&gt;  Seq Scan on users  (cost=0.00..1.04 rows=4 width=3534)
           -&gt;  Index Only Scan using roles_pkey on roles  (cost=0.14..0.16 rows=1 width=4)
                 Index Cond: (id = users_roles.role_id)
  (22 rows)
  </code></pre>