如何添加虚拟字段以查询结果

时间:2012-08-15 09:55:20

标签: ruby-on-rails activerecord

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

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}"

2 个答案:

答案 0 :(得分: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])

答案 1 :(得分: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

将显示所有属性。

提示:我认为您使用了太多的引号,如果属性只存在于一个表中,则不需要为属性添加表名前缀。联接不会提高性能。如果您希望提高绩效,请查看eager loading