I'm trying to display a User's meetings with the following associations.
class User < ApplicationRecord
has_many :panels
has_many :meetings, through: :panels
end
class Meeting < ApplicationRecord
has_one :panel
has_many :users, through: :panels
end
class Panel < ApplicationRecord
belongs_to :Meeting
belongs_to :panel_head, class_name: "User"
belongs_to :panel_member_1, class_name: "User"
belongs_to :panel_member_2, class_name: "User"
end
I can show a Meeting's panel members with the following code:
@meeting.panel.panel_head
@meeting.panel.panel_member_1
@meeting.panel.panel_member_2
How do I go about doing the reverse? If I wanted to display a user's meetings I initially thought to try @user.meetings
but that didn't work. I then tried @user.panel.meetings
but I get thrown an error.
So what I ended up figuring out in case someone comes across this same problem...
In my User model I made the following query:
def meetings
Meeting.joins(:panel).where("panels.panel_head_id = ? OR panels.panel_member_1_id = ? OR panels.panel_member_2_id = ?", id, id, id)
end
I can now use @user.meetings
and be returned with an ActiveRecord Association.