2

我有两个模型

Order has_many State(s)
State belongs_to Order
a State has an attribute named 'kind'

我试图用活动记录表达以下查询:'所有具有 current_state.kind = :something 的订单',

current_state 是订单的最后创建状态。

我还在模型中添加了 :current_state 作为 has_one 关联:

has_one :current_state, -> { order('created_at DESC').limit(1) }, class_name: 'State'

但这无济于事

4

1 回答 1

0

this is the best solution I've found so far

Order.joins(:states).where("states.id IN (SELECT MAX(id) FROM states GROUP BY order_id)").where("states.kind"=>"checkedin")
于 2013-08-09T19:53:35.657 回答