Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Better way to write large SQLs inside rails models?

After using a lot of Arel that Rails 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 Arel methods. I like Arel for little things, but when it get messy I prefer separate the code.

So, is there any advice on how should I be treating my large SQLs inside models? Like, when should I create SQL Views for that (as I seen Rails 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.

I know that some people use the <<-SQL expression

Here is my current example:

Question.from(self.questions
                  .select("questions.id")
                  .select("(NOT (questions.last_active_user_id = #{user.id} OR (COALESCE(ss.updated_at > questions.last_active_at, false) OR COALESCE(ds.updated_at > questions.last_active_at, false))))::integer as active")
                  .select("(((NOT((COALESCE(ss.updated_at > questions.created_at, false) OR COALESCE(ds.updated_at > questions.created_at, false))) AND pages.owner_id = questions.user_id) OR (NOT (COALESCE(ss.updated_at > questions.owner_found_important_at, false) OR COALESCE(ds.updated_at > questions.owner_found_important_at, false)) AND owner_found_important_at is not null AND COALESCE(pages.owner_id <> #{user.id}, true))) AND COALESCE(pages.owner_id <> #{user.id}, true) AND (questions.last_active_user_id <> #{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 < 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 > 0 and questions.open > 0) THEN questions.open ELSE 0 END) as opened_active, SUM(CASE WHEN (questions.owner_active > 0 and questions.open > 0) THEN questions.owner_active ELSE 0 END) as opened_active_owner").first
like image 664
mateusmaso Avatar asked Nov 04 '22 08:11

mateusmaso


1 Answers

Use find_by_sql instead combined with a here document:

Questions.find_by_sql(<<SQL)
select questions.id
  ...
SQL
like image 85
Wes Avatar answered Nov 08 '22 08:11

Wes