好的,这里是 Rails 新手,:D
看起来 has__many :through 是处理多对多关系的最新最佳方式,但我试图保持简单。希望你们当中的一位大师之前已经处理过这种情况:
这是我现在拥有的基本模型设置:
class User < ActiveRecord::Base
has_and_belongs_to_many :products
end
class Product < ActiveRecord::Base
has_and_belongs_to_many :users
has_and_belongs_to_many :clients
end
class Client < ActiveRecord::Base
has_and_belongs_to_many :products
end
本质上,我在系统中有用户,他们可以(通过关联)访问正在创建的许多不同产品,这些产品有很多客户,但客户也可以是许多产品的一部分,并且许多人访问的产品用户。
所有关联都运行良好,但现在我希望用户能够将客户添加到他们的产品中,但只能看到与他们也有权访问的产品相关联的客户。
Scenario:
Given Bob has access to product A and B
And does NOT have access to product C
And and has clients on product B
And wants to add them to product A.
When in product A Bob should see clients from product B in his add list,
And Bob should not see clients from product C
我对 Rails 的粗鲁经验未能让我了解如何最好地构建能够容纳他的客户列表的阵列。
我想的方式是使用@bob.products 来获取 Bob 可以访问的产品,然后访问 .each 并找到与每个产品关联的客户,然后将它们加入一个数组。但这是最好的方法吗?
谢谢!