使用ActiveRecord获取架构的大小

时间:2012-11-27 19:22:25

标签: ruby-on-rails postgresql activerecord schema multi-tenant

我尝试从子域名获取模式的大小.. schema public中的子域名,

在controller.rb上

@account = Account.find_by_subdomain(params[:subdomain])
@itemlist = Account.find(:all,:select => 'subdomain')
@schemasize = ActiveRecord::Base.connection.select_rows(%q{select pg_size_pretty(CAST((SELECT SUM(pg_total_relation_size(table_schema || '.' || table_name) ) FROM information_schema.tables WHERE table_schema = '}+@itemlist.to_s+%q{') As bigint) )  As schema_size}).to_s.gsub(/\D/, '').to_i

获取localhost:3000 / namesubdomain

在命令提示符下输出

 (21.0ms)  select pg_size_pretty(CAST((SELECT SUM(pg_total_relation_size(table
_schema || '.' || table_name) ) FROM information_schema.tables WHERE table_schem
a = '[#<Account subdomain: "namesubdomain">]') As bigint) ) As schema_size

我希望输出命令提示符,例如

   (151.0ms)  select pg_size_pretty(CAST((SELECT SUM(pg_total_relation_size(tabl
e_schema || '.' || table_name) ) FROM information_schema.tables WHERE table_sche
ma = 'namesubdomain') As bigint) ) As schema_size

任何想法?

1 个答案:

答案 0 :(得分:1)

@itemlist.to_s将对象呈现为String。由于对象实际上是一个数组呈现,因为字符串只会输出有关数组而不是内容的信息。你可能想要的是:

@itemlist.first.subdomain

或:

@itemlist.map(&:subdomain).join(" ")