For an example that everyone is familiar with, think of StackOverflow. The user has_many :questions
, has_many :answers
and her questions and answers can be commented on. (Commentary polymorphic).
I want to get all the answers addressed to a specific user, through comments on these questions or user responses:
class User < ActiveRecord::Base has_many :questions has_many :answers has_many :comments has_many :responses, through: [:questions, :answers], source: :comments end class Question < ActiveRecord::Base belongs_to :user has_many :answers has_many :comments, as: :commentable end class Answer < ActiveRecord::Base belongs_to :user belongs_to :question has_many :comments, as: :commentable end class Comment < ActiveRecord::Base belongs_to :commentable, polymorphic: true end
Of course, has_many :responses, through: [:questions, :answers], source: :comments
does not work.
Is there a way for Rails to do this?
Thanks.
Damien
source share