I defined has_one relationship in drivers table
has_one :current_haul
It is associated with hauls table.
And hauls table and drivers table, both of them has organization_id.
I would like to apply the conditions like this
select *
from drivers
join hauls on drivers.organization_id=hauls.organization_id
and drivers.current_haul_id= hauls.id
I can I put this conditions in has_one modifier ?
This is how you can do it, simply add a boolean column on the Haul table. Maybe you will need a unique index to make sure you do not have multiple currents at any given moment.
has_one :current_haul, -> { where(current: true) }, class_name: 'Haul'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With