The simplest approach I can think of is to perform union queries across the relevant schemas. So if you have schemas account_1
and account_2
, you'd do something like SELECT * FROM account_1.users UNION SELECT * FROM account_2.users
.
You can, of course, make this dynamic –?so if you send the affected accounts as part of the params
hash, you'd potentially have something like this:
accounts = Account.find(params[:account_ids])
# assuming the schema name is stored in the accounts.tenant_name column:
sql = accounts.map { |account| "SELECT * FROM #{account.tenant_name}.users" }.join(" UNION ")
users = User.find_by_sql(sql)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…