I’m racking my brain on this complex query for finding related posts:
Here’re my models:
class Post
has_many :post_assignments, :dependent => :destroy
has_many :topics, :through => :post_assignments
end
class PostAssignment
belongs_to :post
belongs_to :topic
end
class Topic
has_many :post_assignments, :dependent => :destroy
has_many :posts, :through => :post_assignments
end
Given that I have a post that may or may not have a topic. I need to find all posts that share a topic in common with this one.
I am already using the squeal gem so that syntax would be preferable.
Sorry I haven’t used the sequeal gem, so don’t know its syntax. I think this would work, but it might not be the best way:
Another way I can think of:
Lastly, you could write the SQL.