如何通过has_one关联的属性对模型进行分组和计数?

时间:2017-03-24 16:53:43

标签: ruby-on-rails ruby activerecord active-record-query

我有一个可以拥有多个设备的电台模型,而每个设备都属于一个电台。每个站都有一个地址,这是一个多态模型:

/app/models/station.rb

class Station < ApplicationRecord
  has_one :address, as: :addressable, dependent: :destroy
  has_many :devices
end

/app/models/device.rb

class Device < ApplicationRecord
  belongs_to :station
  has_one :address, through: :station
end

/app/models/address.rb

# @attr [String] city City name of address
class Address < ApplicationRecord
  belongs_to :addressable, polymorphic: true
end

现在我的图表需要两组数据:

  • 按城市划分的车站数量
  • 按城市划分的设备数量

有效的方法是按城市获取电台数量:

def stations_by_city
  # collect data
  tmp_result = Address.where(addressable_type: 'Station').group(:city).count
  # sort and return top five cities
  result = Hash[tmp_result.sort_by { |_k, v| -v }[0..4]]
  # result = {"City-1"=>17, "City-2"=>14, "City-3"=>14, "City-4"=>12, "City-5=>11} 
end

按城市对设备执行相同操作无法按预期工作。到现在为止我这样做了:

def devices_by_city
  stations = Station.all.includes(:address)
  tmp_result = {}
  # for each unique city
  list_of_cities.uniq.each do |city|
   number_of_devices = 0
   # check all stations
   stations.each do |station|         
     address = station.address
     # check if station is in city
     if address.city == city
       # and add to counter
       number_of_devices += station.devices.count
     end
    end
    # append to resulting hash
    tmp_result[city] = number_of_devices
  end
  result = Hash[tmp_result.sort_by { |_k, v| -v }[0..4]]
end

def list_of_cities
  cities = []
  stations = Station.all.includes(:address)
  stations.each do |station|
    address = station.address
    cities << address.city
  end
    cities
  end
end

我有重复的数据库查找,它通常很难看。如何以更好的方式编写此查询?尝试了各种[.joins,.where,.group] -combinations,但没有工作。将through: :station添加到设备模型有助于其他地方,但没有简化我的问题......

从答案更新

# start join from station model
tmp_result = Station.joins(:address, :devices).group(:city).count

# start join from device model
tmp_result = Device.joins(station: :address).group(:city).count

从设备模型开始加入是最快的:

Timing for old query
  0.530000   0.050000   0.580000 (  0.668664)
Timing for query starting from station model
  0.020000   0.000000   0.020000 (  0.024881)
Timing for query starting from device model
  0.010000   0.000000   0.010000 (  0.009616)

1 个答案:

答案 0 :(得分:0)

您可以在make the property a method or change the property to return a collection.joinsStation模型之间Address以及Device城市的结果,然后应用group_by

count

此查询将执行单个数据库查找以获取所有信息。

您也可以从def devices_by_city_updated temp_result = Station.joins(:address, :devices).group(:city).count result = Hash[tmp_result.sort_by { |_k, v| -v }[0..4]] end 模型开始加入。但是你必须加入嵌套关联才能工作:

Device

您可以在the docs

中查看更多信息