I have this situation:
Stories has many Tasks
Tasks have an integer called hours_left
I need a named scope to find Stories which all its tasks has more than 0 hours left.
Based on this post. I wrote this:
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
But Story.uncompleted
returns an empty array.
Can you help me?