title: Better way to write large SQLs inside rails models?
body: <p>After using a lot of <strong>Arel</strong> that <strong>Rails</strong> provides for sugar code, I am having problems when
  dealing with large and complex SQLs queries that I couldn't do it very well with <strong>Arel</strong> methods.
  I like <strong>Arel</strong> for little things, but when it get messy I prefer separate the code.</p>
  
  <p>So, is there any advice on how should I be treating my large <strong>SQLs</strong> inside models? Like, when should I create <strong>SQL Views</strong> for that (as I seen <strong>Rails</strong> do not provide very well, I have to create a migration for that) or create any separate classes in some folder "sqls" and then call from there.</p>
  
  <p>I know that some people use the <strong>&lt;&lt;-SQL</strong> expression </p>
  
  <p>Here is my current example:</p>
  
  <pre><code>Question.from(self.questions
                    .select("questions.id")
                    .select("(NOT (questions.last_active_user_id = #{user.id} OR (COALESCE(ss.updated_at &gt; questions.last_active_at, false) OR COALESCE(ds.updated_at &gt; questions.last_active_at, false))))::integer as active")
                    .select("(((NOT((COALESCE(ss.updated_at &gt; questions.created_at, false) OR COALESCE(ds.updated_at &gt; questions.created_at, false))) AND pages.owner_id = questions.user_id) OR (NOT (COALESCE(ss.updated_at &gt; questions.owner_found_important_at, false) OR COALESCE(ds.updated_at &gt; questions.owner_found_important_at, false)) AND owner_found_important_at is not null AND COALESCE(pages.owner_id &lt;&gt; #{user.id}, true))) AND COALESCE(pages.owner_id &lt;&gt; #{user.id}, true) AND (questions.last_active_user_id &lt;&gt; #{user.id}))::integer as owner_active")
                    .select("COALESCE(COUNT(answers.id) = 0, true)::integer as open")
                    .joins("LEFT JOIN seens ss ON questions.slide_id = ss.viewed_id AND ss.viewed_type = 'Slide' AND ss.viewer_id = #{user.id}")
                    .joins("LEFT JOIN seens ds ON questions.document_id = ds.viewed_id AND ds.viewed_type = 'Document' AND ds.viewer_id = #{user.id}")
                    .joins("INNER JOIN documents ON documents.id = questions.document_id")
                    .joins("INNER JOIN lists ON lists.id = documents.list_id")
                    .joins("INNER JOIN pages ON pages.id = lists.page_id")
                    .joins("LEFT OUTER JOIN answers ON answers.question_id = questions.id")
                    .where("questions.reports_count &lt; 2")
                    .group("questions.id, active, owner_active")
                    .as('questions'))
                    .select("SUM(questions.active) as active, SUM(questions.owner_active) as owner_active, SUM(questions.open) as opened, COUNT(questions.id) as total, SUM(CASE WHEN (questions.active &gt; 0 and questions.open &gt; 0) THEN questions.open ELSE 0 END) as opened_active, SUM(CASE WHEN (questions.owner_active &gt; 0 and questions.open &gt; 0) THEN questions.owner_active ELSE 0 END) as opened_active_owner").first
  </code></pre>