我试图找出模型之间建模关系的最佳逻辑方法。 我有4个型号:
此处用户拥有许多产品,SlackTeams和组织,而SlackTeam属于User并且有一个组织。组织应属于User和SlackTeam。我在逻辑上是否正确?
工作流程如下:
我错过了什么吗?
答案 0 :(得分:0)
class User
has_many :users_ogranizations
has_many :organizations, through: :users_organizations
has_many :products, through: :organizations
end
class Product
belongs_to :organization
end
class Organization
has_many :users_ogranizations
has_many :users, through: :users_organizations
has_many :products
end
class UsersOrganization
belongs_to :user
belongs_to :organization
end
# i'd rather use slack profile than team, because Organization and Team
# already connotes the same thing.
class SlackProfile
end
您可以随意处理用户的登录,但我更喜欢一种身份验证服务。现在,用户可以访问属于该组织的所有产品,然后您可以使用以下内容过滤产品:
current_user.products.where(organization: Organization.last)