活动记录多个连接返回错误的值并重复Rails

时间:2014-03-02 14:17:17

标签: sql ruby-on-rails ruby postgresql

我在Rails 4中对自定义类进行了以下查询:

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)

2 个答案:

答案 0 :(得分:9)

Paritosh的回答是正确的,但使用ActiveRecord,你可以打电话.distinct

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

答案 1 :(得分:1)

如果您不想要重复记录,可以执行类似

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