'Activerecord has_many :through through multiple models

I'm trying to access all comments from a given user with user.comments. The query is to go through two different models, which likely both return results. My relations are set up as follow:

class User < ActiveRecord::Base
  has_many :organisers
  has_many :participants
  has_many :comments, through: :participants / :organisers (see explenation below)
end

class Organiser < ActiveRecord::Base
  belongs_to :user
end

class Participant  < ActiveRecord::Base
  belongs_to :user
end

class Comment < ActiveRecord::Base
  belongs_to :organiser
  belongs_to :participant
end

A comment is validated to belong to either a participant, or an organiser.

I'm not sure how to go about this. I've tried

has_many :comments, through: :participants
has_many :comments, through: :organisers

and

has_many :comments, through: [:organisers, :participants]

But that last one isn't rails. Is there a proper way to do this? Thanks!



Solution 1:[1]

has_many :comments, ->(user) {
  unscope(where: :user_id).
  left_joins(:organizer, :participant).
  where('organizers.user_id = ? OR participants.user_id = ?', user.id, user.id)
}

The unscope is to remove the comments.user_id = ? clause (which is added by default when you define a has_many relation). The left_joins is called on Comment, so you need to pass in the relation names as defined on Comment, hence the singulars in this example.

Solution 2:[2]

I found a solution after many tries. You can use a scope with param in your last has_many sentence in the User model:

has_many :comments, -> (user) {where organiser: user.organisers}, through: :participants

The "user" param represet the User object whom is calling the comments method.

Solution 3:[3]

Since we couldn't use has_many, through here because comments come from both of organisers and participants. I just think there are 2 solutions here:

Solution #1 Define comments method:

class User < ActiveRecord::Base
  def comments
    Comment.joins([{organiser: :user}, {participant: :user}])
           .where(users: {id: self.id})
  end
end

So then your query to find comments is:

User.first.comments

Solution #2 Use scope in Comment

class Comment < ActiveRecord::Base
  scope :from_user, -> (user) { 
    joins([{organiser: :user}, {participant: :user}]).where(users: {id: user.id}) 
  }
end

So your query will be like:

user = User.first
comments = Comment.from_user(user)

Solution 4:[4]

Since we couldn't use has_many, through here because comments come from both of organizers and participants. I just think there are 2 solutions here:

Basically you can still change the foreign key to accept the self.id automatically with Rails here

User.first.comments

class User
  has_many :comments, -> { joins([{ organiser: :user }, { participant: :user }]) }, through: :participants, foreign_key: "users.id"
end

Solution 5:[5]

For anyone coming across this using polymorphic associations, the following worked for me inspired by magni- and Carlos Jimenez' answers:

has_many :comments, -> (user) {
  unscope(where: :user_id).
  where(commentable: [user.organizers, user.participants])
}

Solution 6:[6]

I believe your associations would be confused, as user.comments wouldn't know whether it's going through Participant or Organiser, so the best option would be to declare two different joins (with different names):

http://guides.rubyonrails.org/association_basics.html#self-joins

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 magni-
Solution 2 Carlos Jiménez
Solution 3 Hieu Pham
Solution 4 Bernie Chiu
Solution 5
Solution 6 Tim K.