每个并在控制器Rails中使用%w

时间:2013-04-07 06:58:46

标签: ruby-on-rails ruby-on-rails-3.2 schema

我有以下哪种方法效果很好:

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
schema1上的

有14个userschema2有0个用户。

更新

我尝试将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

我想统计来自schema1schema2

的用户
@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

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

1 个答案:

答案 0 :(得分:0)

试试这个

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

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