Better way to write large SQLs inside rails models?

泪湿孤枕 提交于 2019-12-21 15:01:34

问题


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

回答1:


Use find_by_sql instead combined with a here document:

Questions.find_by_sql(<<SQL)
select questions.id
  ...
SQL


来源:https://stackoverflow.com/questions/13885040/better-way-to-write-large-sqls-inside-rails-models

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!