nulltek
nulltek

Reputation: 3337

Querying a has_many_through join table in Rails

I have an app in Rails 5 where I have the following relationships:

User model

has_many :consult_users
has_many :consults, through: :consult_users

Consult Model

 has_many :consult_users
 has_many :users, through: :consult_users

ConsultUser Model (Join Table)

  belongs_to :consult
  belongs_to :user

In a controller I need to list out all of the consults for a given user. So in the English list all consults which have a join record consult_users and the consult_users.user_id equals the current_user.id

I'm having a really tough time mowing through this and was wondering if anyone could give me a tip on how to write this query to get an array of all Consults which meet this query.

I've tried raw SQL and also ActiveRecord but I am hitting a wall.

Any help is greatly appreciated.

Upvotes: 1

Views: 96

Answers (1)

Taryn East
Taryn East

Reputation: 27747

You should just be able to use the associations for that eg:

current_user.consults

Upvotes: 2

Related Questions