Reputation: 544
I have a projects model that has the following
has_many :project_topics
has_many :topics, :through => :project_topics
I need a way to be able to query a topic id
and have it return all the projects that have that topic. Any ideas?
def projects_by_topic(topic)
end
Upvotes: 1
Views: 23
Reputation: 19031
I would try something like this. And check out Rails guild on ActiveRecord.
def projects_by_topic(topic)
project_topics = ProjectTopics.where(topic_id: topic.id)
project_topics_ids = project_topics.map {|pt| pt.id }
projects = Project.where(project_topic_id: project_topics_ids)
end
Upvotes: 1