2012-11-27 20 views
1

我嘗試從子域的名稱獲取架構的大小。在公共模式的子域的名稱 ,獲取規模架構使用ActiveRecord

上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 = '}[email protected]_s+%q{') As bigint)) As schema_size}).to_s.gsub(/\D/, '').to_i 

獲得本地主機: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 

我想OU輸入命令提示符如

(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

@itemlist.to_s將呈現該對象作爲字符串。由於對象實際上是一個數組渲染,因爲字符串將只輸出有關數組而不是內容的信息。你可能想要的是兩種:

@itemlist.first.subdomain 

或:

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

錯誤輸出.. (4.0ms)選擇pg_size_pretty(CAST((SELECT SUM(pg_total_relation_size(table_ 架構||」。 '|| table_name))FROM information_schema.tables WHERE table_schema ='#')作爲bigint))作爲schema_size – GeekToL

+0

顯然,對象的默認渲染不會定義'to_s';)使用'子域'而不是 - 更新的答案。 – PinnyM

+0

非常感謝你給我上課。這是非常有幫助:) – GeekToL