Extract duplicated query

This includes a bit of a simplification which assumes that there aren't
any orphan taggings for a user.
This commit is contained in:
Katrina Owen 2013-03-01 21:27:58 -05:00
parent 8b0f3e986a
commit 4dff7538ea
2 changed files with 29 additions and 24 deletions

View file

@ -0,0 +1,26 @@
module Stats
class UserTagsQuery
attr_reader :user
def initialize(user)
@user = user
end
def result
user.todos.find_by_sql([sql, user.id])
end
private
def sql
<<-SQL
SELECT tags.id as id
FROM tags, taggings, todos
WHERE tags.id = taggings.tag_id
AND taggings.taggable_id = todos.id
AND todos.user_id = ?
SQL
end
end
end