【发布时间】:2013-04-06 19:53:55
【问题描述】:
我有多租户应用程序轨道,并尝试为每个帐户获取架构 posgresql 的大小磁盘。最初,我在视图上尝试了一下,大致是这样的:
<% for account in @accounts %>
<td>
<%= number_to_human_size(@schemasize = (ActiveRecord::Base.connection.select_rows(%q{select sum(pg_total_relation_size(table_schema || '.' || table_name)) from information_schema.tables where table_schema in ('} +account.schema+ %q{') group by table_schema}).to_s.gsub(/\D/, '').to_f + account.size_logo.to_f), :locale=> 'en') %>
</td>
<% end %>
浏览器输出
| schema | usage |
|_________|________|
| schema1 | 259 KB |
| schema2 | 294 KB |
但它没有插入数据库(在公共架构上)。 现在,我希望它存储在数据库中。
这是每个帐户的架构大小磁盘的控制器:
@accounts.each do |t|
select_size = "select sum(pg_total_relation_size(table_schema || '.' || table_name)) from information_schema.tables where table_schema = " + t.schema.to_s + " group by table_schema"
@a = ActiveRecord::Base.connection.select_rows(select_size)
@b = @a.to_s.gsub(/\D/, '').to_f
@c = t.size_logo.to_f
@d = @b + @c
@cek_schema = Usage.find_by_account_id(t.id)
if @cek_schema.nil?
Usage.create(:account_id => t.id, :size => @d)
else
@cek_schema.destroy
Usage.create(:account_id => t.id, :size => @d)
end
end
我收到一个错误
ActiveRecord::StatementInvalid in AccountsController#kapasitas
PG::Error: ERROR: column "schema1" does not exist
LINE 1: ... from information_schema.tables where table_schema = schema1
^
: select sum(pg_total_relation_size(table_schema || '.' || table_name)) from information_schema.tables where table_schema = schema1 group by table_schema
谁能告诉我是什么错误?
- 获取架构大小的磁盘并存储在数据库中
帮帮我。
谢谢
【问题讨论】:
-
t.schema.to_s 没有被引用。 Pg 认为这是一个表名。用一个 ?占位符或在其周围加上单引号。
标签: ruby-on-rails postgresql activerecord ruby-on-rails-3.2