2014-03-02 47 views
3

我在軌道4,5得到下面的查詢我的自定義類:活動記錄多個聯接返回錯誤的值和重複的Rails

User.joins(:roles).joins(:events).joins(:booths).group("users.id, roles.id") 

然而,這返回錯誤的用戶的多個副本。

我希望能夠回到用戶,用戶的角色,事件和攤位

下面是用戶模型協會:

after_create :assign_default_role 
    rolify :before_add => :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 => 2.weeks 

    has_and_belongs_to_many :events, :autosave => true 

    has_many :venues 
    has_many :booths 
    has_many :webcasts 
    # has_many :events, through: :venues 
    has_many :from_user_chats, :foreign_key => 'from_user_id', :class_name => 'Chat' 
    has_many :to_user_chats, :foreign_key => 'to_user_id', :class_name => 'Chat' 
    has_one :uploaded_file, as: :imageable, dependent: :destroy 
    accepts_nested_attributes_for :uploaded_file, :reject_if => proc { |attributes| attributes['assets'].blank? } 

有什麼,我在這裏失蹤?

我需要做些什麼才能讓所有用戶從這些其他表中獲得這些附加信息?

輸出的

explain

=> 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) 
    -> Nested Loop (cost=6.01..6.65 rows=1 width=3538) 
     -> Merge Join (cost=5.86..6.47 rows=1 width=3538) 
       Merge Cond: (users_roles.user_id = users.id) 
       -> Nested Loop (cost=4.78..281.23 rows=963 width=16) 
        -> Nested Loop (cost=4.49..232.77 rows=54 width=8) 
          Join Filter: (events_users.event_id = events.id) 
          -> Nested Loop (cost=4.49..227.48 rows=54 width=12) 
           -> Index Only Scan using index_booths_on_user_id on booths (cost=0.13..16.22 rows=6 width=4) 
           -> Bitmap Heap Scan on events_users (cost=4.36..35.12 rows=9 width=8) 
             Recheck Cond: (user_id = booths.user_id) 
             -> 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) 
          -> Materialize (cost=0.00..2.06 rows=4 width=4) 
           -> Seq Scan on events (cost=0.00..2.04 rows=4 width=4) 
        -> 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) 
       -> Sort (cost=1.08..1.09 rows=4 width=3534) 
        Sort Key: users.id 
        -> Seq Scan on users (cost=0.00..1.04 rows=4 width=3534) 
     -> 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) 
+0

包括該公司的輸出de'User.joins(:roles).joins(:events).joins(:booths).group(「users.id,roles.id」)。explain' –

+0

@ParitoshPiplewar我剛剛添加 –

回答

1

如果你不想重複的記錄,你可以這樣做

User.joins(:roles,:events,:booths).select("distinct on(users.id,roles.id) users.*") 
8

Paritosh的答案是正確的,但與ActiveRecord的,你可以叫.distinct

User.joins(:roles,:events,:booths).distinct