1

我有一个范围,用于查找 call_status 已打开且 unit_id 为 nil 的呼叫记录。

scope :unassigned_calls, where(:call_status => "open", :unit_id => nil).order("id ASC")

我最近设置了一个 has_many 关系,其中 unit_id 不再在调用模型上使用,而是在 call_unit 模型连接表上使用了一个名为 unit_ids 的字段。

如何将范围或 lambda 表示到它包含连接表中的 unit_ids 的位置?

4

1 回答 1

0

我不相信 AREL 允许您调用外部联接。尝试写出 SQL

scope :unassigned_calls, joins("left outer join call_unit on call.id=call_unit.call_id").where("call_unit.unit_id is null").order("id ASC")

我在这里假设连接表有一个 call_id 列,这可能不准确,但希望它能为您提供必要的框架

于 2012-08-21T18:58:35.900 回答