【发布时间】:2020-04-28 11:16:24
【问题描述】:
我有这些模型:
class Product < ApplicationRecord
has_many :units
end
class Unit < Product
belongs_to :product
end
上面的模型只使用一个表,称为'products'表。
create_table :products do |t|
t.string :code
t.integer :product_id
t.string :type
t.string :status
...
end
我想要的是获取所有units,其中products.status 是active。
我试过Unit.joins(:product).where(products: { status: 'active' }),但没有成功
这是我拥有的当前数据
| id | type | product_id | status |
|----|:-------:|-----------:|---------|
| 1 | Product | null | deleted |
| 2 | Product | null | active |
| 3 | Unit | 1 | active |
| 4 | Unit | 1 | active |
| 5 | Unit | 2 | active |
我只想得到
| id | type | product_id | status |
|----|:-------:|-----------:|---------|
| 5 | Unit | 2 | active |
【问题讨论】:
标签: ruby-on-rails activerecord ruby-on-rails-5