ActiveRecord:具有Sum,Join,Group By和Select多个字段的复杂查询

时间:2014-11-18 07:09:34

标签: sql ruby-on-rails postgresql ruby-on-rails-4 activerecord

我已经为这个问题花了几天时间,但仍然没有找到解决方案。

有我的模型

class UserObject < ActiveRecord::Base
  self.table_name = "user_objects"
  belongs_to :user, class_name: 'User'
  belongs_to :the_object, class_name: 'Object'
end

class Object < ActiveRecord::Base
  self.table_name = 'objects'

  has_many :user_objects, class_name: 'UserObject', foreign_key: :the_object_id, inverse_of: :the_object
  has_many :users, through: :user_objects
end

class User < ActiveRecord::Base
  self.table_name = "users"
end

这是我的架构

  create_table "objects", force: true do |t|
    t.float    "importance",        default: 0.5, null: false
  end

  create_table "user_objects", force: true do |t|
    t.integer  "user_id"
    t.integer  "the_object_id"
    t.float    "score",              default: 0.0,   null: false
  end

  create_table "users", force: true do |t|
    t.string   "first_name"
    t.string   "last_name"
  end

我需要查询选择objects.importanceuser_objects.score的总和。但我也必须选择仅查询属于objectsuser1的{​​{1}}。

我为select objects.importance

写了一个查询
user2

它已转换为

Object.select("objects.importance").joins(:user_objects).
                                    where(user_objects: {user_id: [user1_id,user2_id]}).
                                    group(objects: :id).
                                    having('COUNT("user_objects"."id")=2')

当我执行此查询时,我收到了此回复

SELECT objects.importance FROM "objects" INNER JOIN "user_objects" ON "user_objects"."the_object_id" = "objects"."id" WHERE "user_objects"."user_id" IN (2, 7) GROUP BY "objects"."id" HAVING COUNT("user_objects"."id")=2

响应中的对象数量正常。但我仍然不知道如何计算[#<Object id: nil, importance: 0.5>, #<Object id: nil, importance: 0.5>] 的查询总和。下一个查询无效

user_objects.score

我期待回应这样的事情

Object.select("objects.importance, SUM(user_objects.score)").
       joins(:user_objects).
       where(user_objects: {user_id: [user1_id,user2_id]}).
       group(objects: :id).having('COUNT("user_objects"."id")=2')

我非常感谢您在解决这个问题时能给我的任何帮助。

1 个答案:

答案 0 :(得分:0)

Ок。我找到了解决方案。这是正确的查询。

Object.joins(:user_objects).
       where(user_objects: {user_id: [user1_id,user2_id]}).
       group(objects: :id).
       having('COUNT("user_objects"."id")=2').
       pluck("objects.importance, SUM(user_objects.score)")

问题出现在select方法中,因为它创建了一个调用此方法的类的对象。因此,不可能在少数模型的一个select属性中进行选择。但是pluck返回带有字段的结果数组所以我们可以从不同的表中选择字段。 就是这样。这么简单!

相关问题