【发布时间】:2015-10-31 19:40:13
【问题描述】:
我有以下模型:项目、开发人员和主题。我还有一些连接表:ProjectTopic 和 DeveloperTopic。
我希望能够找到与给定项目实例具有相同主题的所有开发人员。我可以通过 Project.first.topics 获取项目的主题,但我不能使用主题列进行 Developer.where 查询,因为它不存在 - 它只是通过连接表/通过关联创建的方法。
如何找到与给定项目具有相同主题的所有开发人员?
class Developer < ActiveRecord::Base
has_many :projects
has_many :developer_technologies
has_many :technologies, through: :developer_technologies
has_many :developer_topics
has_many :topics, through: :developer_topics
end
class Project < ActiveRecord::Base
has_many :technologies, through: :project_technologies
has_many :topics, through: :project_topics
has_many :project_technologies
has_many :project_topics
belongs_to :customer
belongs_to :developer
end
class ProjectTopic < ActiveRecord::Base
belongs_to :project
belongs_to :topic
end
class Topic < ActiveRecord::Base
has_many :developers
has_many :developer_technologies, through: :developers
has_many :projects
has_many :project_technologies, through: :project
end
class DeveloperTopic < ActiveRecord::Base
belongs_to :developer
belongs_to :topic
end
【问题讨论】:
标签: ruby-on-rails join activerecord has-many-through model-associations