我有以下哪种方法效果很好:
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个user
,schema2
有0个用户。
更新
我尝试将1位用户添加到schema2
现在schema1
有14个user
,schema2
有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
为什么只有最后一个模式才能获得用户表的计数?
答案 0 :(得分:0)
试试这个
@user = 0
schemas = Account.all.map(&:schema)
with_in_schemas :only => schemas do
@user += User.count
end