Geo*_*hev 3 ruby-on-rails ruby-on-rails-4
我在 drivers 表中定义了 has_one 关系
has_one :current_haul
Run Code Online (Sandbox Code Playgroud)
它与运输表相关联。
还有运输表和驾驶员表,它们都有organization_id。
我想应用这样的条件
select *
from drivers
join hauls on drivers.organization_id=hauls.organization_id
and drivers.current_haul_id= hauls.id
Run Code Online (Sandbox Code Playgroud)
我可以把这个条件放在has_one修饰符中吗?
小智 5
您可以这样做,只需在Haul表上添加一个布尔列即可。也许您需要一个唯一的索引来确保在任何给定时刻都没有多个电流。
has_one :current_haul, -> { where(current: true) }, class_name: 'Haul'