ActiveRecord 使用 Includes 连接表

ActiveRecord join tables with Includes

如何使用 Ruby ActiveRecord 执行上面的查询?

SELECT p.name, c.ios FROM public.projects p
INNER JOIN public.controls c
ON c.company_id = p.company_id

我试过了

Project.includes(company::controls)
Project.joins(:company => controls)

但都失败了

Project Model: { id, name, company_id, type, created_at, updated_at }
Company Model: { id, name, created_at, updated_at }
Control Model: { id, ios, android, macos, company_id, created_at, updated_at}

如果它是 INNER JOIN,您需要 table1: :table2,假设您的模型是正确的,您需要以下内容

Project.includes(company: :controls)
Project.joins(company: :controls)