我有这种情况:
Stories has many Tasks
Tasks have an integer called hours_left
我需要一个命名范围来查找所有任务剩余时间超过 0 小时的故事。
基于这篇文章。我写了这个:
class Story
has_many :tasks
named_scope :uncompleted, {
:joins=>["INNER JOIN tasks ON tasks.story_id = stories.id"],
:group=> 'stories.id',
:select=>'stories.*, SUM(tasks.hours_left) AS sum_amount',
:having=>"sum_amount > 0"
}
end
但Story.uncompleted
返回一个空数组。
你能帮助我吗?