2013-04-07 21 views
0

我有一個運作良好的情況如下:如每個使用%w的控制器的Rails

@user => 14 

我想架構名稱從賬戶獲得

with_in_schemas :only => %w{schema1 schema2} do 
    @user = User.count 
end 

和輸出,我有這個:

@accounts = Account.all 
@accounts.each do |t| 
with_in_schemas :only => t.schema do 
    @user = User.count 
end 
end 

但輸出爲零

@user => 0 

schema1有14個userschema2有0個用戶。

UPDATE

我試過1個用戶添加到schema2

現在schema1有14 userschema2有1個用戶。

和控制器

@accounts = Account.all 
@accounts.each do |t| 
    with_in_schemas :only => t.schema do 
     @user = User.count 
    end 
end 

和輸出

@user => 1 

我想從schema1計數的用戶和schema2

@accounts = Account.all 
@count_user = 0 
    @accounts.each do |t| 
     with_in_schemas :only => t.schema do 
     @user = User.count 
     end 
     @count_user = @count_user + @user 
    end 

和輸出

@count_user => 2 

爲什麼只有最後一個schema得到用戶表的數量?

+0

什麼是't.schema'?你真的有這個名字的模式嗎?如果是這樣,用戶表是否有任何記錄? – 2013-04-07 07:07:27

+0

那麼't.schema'返回一個數組還是一個對應於模式的單個字符串?更正了 – 2013-04-07 07:17:46

+0

,我的問題已更新。 – 2013-04-07 07:32:28

回答

0

試試這個

@user = 0 
schemas = Account.all.map(&:schema) 

with_in_schemas :only => schemas do 
    @user += User.count 
end 
+0

輸出@user => 2,我真的很困惑。 – 2013-04-07 07:55:28

+0

@anonymousxxx,你確定你還有15個用戶嗎? – Mischa 2013-04-07 07:58:11

+0

是的,我試試這個'with_in_schemas:only =>%w {schema1 schema2}做 @user = User.count end'output @user => 15 ..好的我改變了我的問題,怎麼可以'account'模式'schema1','schema2')轉換爲'%w',所以它就像'w%{schema1 schema2}' – 2013-04-07 08:06:27