简体   繁体   中英

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.

您应该只能够为此使用关联,例如:

current_user.consults

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM