3

为了提高性能,我写了以下内容:

a = Address.joins(:vacancies).select('"addresses"."city", COUNT (*) AS total_count').group('"addresses"."city"').order('total_count DESC')

代替:

a = Address.all.keep_if{ |a| a.vacancies.all_active.size > 0 }.uniq{ |a| a.city }.map{ |a| [a.city, a.vacancies.all_active.size] }.sort{ |aa, ab| aa[1] <=> ab[1] }.reverse

但是,该total_count字段不会显示在结果中。如果我在 sqlite 中运行查询,它会显示出来。我向地址模型添加了一个虚拟属性,但这没有帮助。如何解决这个问题?

另外,这是我的 Rails 控制台输出:

1.9.3p194 :010 > a = Address.joins(:vacancies).select("addresses.city, count(*) as total_count").group("addresses.city").order("total_count DESC")
  Address Load (3.9ms)  SELECT addresses.city, count(*) as total_count FROM "addresses" INNER JOIN "vacancies" ON "vacancies"."address_id" = "addresses"."id" GROUP BY addresses.city ORDER BY total_count DESC
 => [#<Address city: "Amsterdam">, #<Address city: "Rotterdam">, #<Address city: "Utrecht">, #<Address city: "Den haag">, #<Address city: "Delft">, #<Address city: "Hilversum">, #<Address city: "Amstelveen">, #<Address city: "Rijswijk">, #<Address city: "Breda">, #<Address city: "Zwolle">, #<Address city: "Tilburg">, #<Address city: "Amersfoort">, #<Address city: "Amsterdam centrum">, #<Address city: "Eindhoven">, #<Address city: "Next nature netwerk, amsterdam">, #<Address city: "Almere">, #<Address city: "Ameide">, #<Address city: "Arnhem">, #<Address city: "Baarlo">, #<Address city: "Capelle a/d ijssel">, #<Address city: "De lier">, #<Address city: "Leiden">, #<Address city: "Reigerstraat 16, breda">, #<Address city: "Rottedam">, #<Address city: "'s-hertogenbosch ">, #<Address city: "Alkmaar">, #<Address city: "Alphen aan den rijn">, #<Address city: "Amsterdam singel 137">, #<Address city: "Amsterdam, keizergracht">, #<Address city: "Barendrecht">, #<Address city: "Den haag e.o.">, #<Address city: "Deventer">, #<Address city: "Dordrecht">, #<Address city: "Groningen">, #<Address city: "Gummo, amsterdam">, #<Address city: "Hillversum">, #<Address city: "Houten">, #<Address city: "Lisse">, #<Address city: "Moti, breda">, #<Address city: "Nijmegen">, #<Address city: "Rotterdam ">, #<Address city: "Tuil (waardenburg)">, #<Address city: "Zaltbommel">] 
1.9.3p194 :011 > a.first.total_count
 => nil 
1.9.3p194 :012 > a.first.to_json
 => "{\"city\":\"Amsterdam\",\"total_count\":null}"
4

2 回答 2

5

首先,删除虚拟属性(我假设您的意思是您attr_accessor :total_count的模型中有)。

Active Record 的inspect方法(在控制台中生成输出)是硬连线的,它只显示有关它所期望的属性的信息,即对应于表上实际列的那些信息。仅因为您在 select 子句中添加了某些内容而存在的属性不会显示。他们还在那里 - 你可以做

a.first.total_count

或者

a.first.attributes

看见了。在您的控制台会话a.first.total_count中不起作用,因为您attr_accessor正在隐藏数据库属性。

to_json方法的相似之处在于默认情况下它会序列化它知道的属性。您可以通过执行包含total_count在您的输出中

a.to_json(:methods => [:total_count])
于 2012-08-25T09:39:28.693 回答
2

您的查询:

@result = Address.joins(:vacancies).select("addresses.city, count(*) as total_count").group("addresses.city").order("total_count DESC")

第一个元素的总数:

@result.first.total_count

如果您在控制台中看不到 total_count 属性

@result.first.to_json

将向您显示所有属性。

提示:我认为您使用了太多引号,并且如果属性仅存在于一个表中,则不需要在表名前为属性添加前缀。联接不会提高性能。如果您想提高性能,请查看急切加载

于 2012-08-15T10:27:11.947 回答