Maybe you can move the sql to a scope, or a method in the Project.rb model?