2016-07-14 69 views
0

我定義在驅動表滑軌HAS_ONE條件

has_one :current_haul 

它與拖拉表相關聯HAS_ONE關係。
和拖運表和驅動表,他們都有organization_id

我想申請的條件是這樣

select * 
from drivers 
join hauls on drivers.organization_id=hauls.organization_id 
and drivers.current_haul_id= hauls.id 

我,我可以把這個條件has_one修改?

+0

您是否檢查過[Active Record Associations docs](http://guides.rubyonrails.org/association_basics.html#scopes-for-has-one)?爲了使它成爲一個動態條件,你可以使用lambdas,如[這個SO回答](http://stackoverflow.com/a/2462397/2116518)所示。 –

回答

0

您可以在驅動程序模型中添加如下所示的關聯。

HAS_ONE:current_haul, - > {其中(電流:真)。首先},CLASS_NAME:「牽引」

我在這裏假設一個布爾位存在於你的牽引模型來檢查當前狀態。