0

我有三个模型:

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
4

1 回答 1

0

许多人提出了这个要求,您会在Rails Lighthouse Ticket #8994中看到一些开发人员试图合并nested_has_many_through 插件的功能的讨论。如果您使用的是 Rails 2.3,nested_has_many_through 插件可能适合您,否则在 Rails 3 中您可以编写ARel查询来查找正确的研究集。

于 2010-11-01T02:39:41.607 回答