有表审计和audit_statuses,需要获取两者中不常见的数据。我已经有一个在postgresql中工作的查询,如何将其转换为Ruby活动记录。
以下是Psql查询
SELECT a.name as status,count(b.audit_status_id) as count
FROM audit_statuses as a,audits as b
WHERE a.id=b.audit_status_id
GROUP BY a.name;
答案 0 :(得分:2)
您的关联模型是审核belongs_to
审核_status,审核_status has_many
审核,是不是?并且您想要计算审计所拥有的Audit_statuses的数量,不是吗?
如果是,您应该使用counter_cache
阅读有关belongs_to Association Reference #counter_cache
虽然在模型上指定了:counter_cache选项 包括belongs_to声明,必须添加实际列 到相关的模型。在上面的例子中,您需要添加一个 列为audit_status模型的count_audit列
通过运行:
将count_audit
属性添加到audit_statuses
表
rails g migration AddCountAuditToAuditStatuses count_audit:integer
并运行rake db:migrate
在您的模型上看起来像:
class Audit < ActiveRecord::Base
attr_accessible :audit_status_id, :status
belongs_to :audit_status, :counter_cache => :count_audit
end
class AuditStatus < ActiveRecord::Base
attr_accessible :count_audit, :name
has_many :audits
end
示例创建audit_status
irb(main):001:0> audit_status = AuditStatus.create!(:name => "John Noe")
←[1m←[36m (0.0ms)←[0m ←[1mBEGIN←[0m
←[1m←[35mSQL (114.0ms)←[0m INSERT INTO "audit_statuses" ("count_audit", "created_at", "name", "up
dated_at") VALUES ($1, $2, $3, $4) RETURNING "id" [["count_audit", nil], ["created_at", Fri, 06 Jun
2014 15:17:49 WIB +07:00], ["name", "John Noe"], ["updated_at", Fri, 06 Jun 2014 15:17:49 WIB +07:0
0]]
←[1m←[36m (17.0ms)←[0m ←[1mCOMMIT←[0m
=> #<AuditStatus id: 1, name: "John Noe", count_audit: nil, created_at: "2014-06-06 08:17:49", updat
ed_at: "2014-06-06 08:17:49">
创建两条审核记录,并获取audit_status.id
audit_status_id
irb(main):002:0> audit = Audit.create!({:audit_status_id => audit_status.id, :status => true})
←[1m←[35m (0.0ms)←[0m BEGIN
←[1m←[36mSQL (6.0ms)←[0m ←[1mINSERT INTO "audits" ("audit_status_id", "created_at", "status", "up
dated_at") VALUES ($1, $2, $3, $4) RETURNING "id"←[0m [["audit_status_id", 1], ["created_at", Fri,
06 Jun 2014 15:19:00 WIB +07:00], ["status", true], ["updated_at", Fri, 06 Jun 2014 15:19:00 WIB +07
:00]]
←[1m←[35mAuditStatus Load (1.0ms)←[0m SELECT "audit_statuses".* FROM "audit_statuses" WHERE "audi
t_statuses"."id" = 1 LIMIT 1
←[1m←[36mSQL (2.0ms)←[0m ←[1mUPDATE "audit_statuses" SET "count_audit" = COALESCE("count_audit",
0) + 1 WHERE "audit_statuses"."id" = 1←[0m
←[1m←[35m (1.0ms)←[0m COMMIT
=> #<Audit id: 1, audit_status_id: 1, status: true, created_at: "2014-06-06 08:19:00", updated_at: "
2014-06-06 08:19:00">
irb(main):003:0> audit = Audit.create!({:audit_status_id => audit_status.id, :status => false})
←[1m←[36m (1.0ms)←[0m ←[1mBEGIN←[0m
←[1m←[35mSQL (1.0ms)←[0m INSERT INTO "audits" ("audit_status_id", "created_at", "status", "update
d_at") VALUES ($1, $2, $3, $4) RETURNING "id" [["audit_status_id", 1], ["created_at", Fri, 06 Jun 2
014 15:19:23 WIB +07:00], ["status", false], ["updated_at", Fri, 06 Jun 2014 15:19:23 WIB +07:00]]
←[1m←[36mAuditStatus Load (1.0ms)←[0m ←[1mSELECT "audit_statuses".* FROM "audit_statuses" WHERE "
audit_statuses"."id" = 1 LIMIT 1←[0m
←[1m←[35mSQL (0.0ms)←[0m UPDATE "audit_statuses" SET "count_audit" = COALESCE("count_audit", 0) +
1 WHERE "audit_statuses"."id" = 1
←[1m←[36m (0.0ms)←[0m ←[1mCOMMIT←[0m
=> #<Audit id: 2, audit_status_id: 1, status: false, created_at: "2014-06-06 08:19:23", updated_at:
"2014-06-06 08:19:23">
因此,您只需调用audit_status的记录:
irb(main):004:0> @audit_statuses = AuditStatus.all
←[1m←[35mAuditStatus Load (1.0ms)←[0m SELECT "audit_statuses".* FROM "audit_statuses"
=> [#<AuditStatus id: 1, name: "John Noe", count_audit: 2, created_at: "2014-06-06 08:17:49", update
d_at: "2014-06-06 08:17:49">]
答案 1 :(得分:0)
@audit_status = Audit.joins( :audit_status).select("name as status,count(audit_status_id) as count").group(:name)