0

我有以下效果很好:

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

和输出如

@user => 14

我想从帐户中获取架构名称,我有这个:

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

但输出为零

@user => 0

schema114 个userschema20 个用户。

更新

我尝试添加 1 个用户schema2

现在schema1有 14 个userschema21 个用户。

在控制器上

@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

为什么只有最后一个模式得到用户表的计数?

4

1 回答 1

0

试试这个

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

with_in_schemas :only => schemas  do
  @user += User.count
end
于 2013-04-07T07:53:10.230 回答