私は3つのモデルを持っています:
class Tenant < ActiveRecord::Base
has_many :sites
end
class Site < ActiveRecord::Base
belongs_to :tenant
has_and_belongs_to_many :users
end
class User < ActiveRecord::Base
has_and_belongs_to_many :sites
end
サイトとユーザーには、結合テーブル (sites_users) があります。
すべてのサイトには n 人のユーザーがいます。すべてのテナントには n 個のサイトがあります。
特定のサイトのユーザーのリストを取得するには、次のように簡単です。
t = Tenant.first
s = t.sites.first
s.users
しかし、そのテナントの下にあるすべてのサイト間のすべてのユーザーのリストを提供するためにテナントに追加できる関連付けはありますか? 私ができるように:
t = Tenant.first
t.users