本文介绍了更好的方式写在轨道模型内的大型SQL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用了大量的 Arel 之后, Rails 提供了糖代码,我遇到了问题,当
处理大型和复杂的SQL查询时,使用 Arel 方法很好。
我喜欢 Arel 的小东西,但是当它变得凌乱,我更喜欢分开的代码。

After using a lot of Arel that Rails provides for sugar code, I am having problems whendealing 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.

如何处理我的大型 SQL 模型?像,我应该为什么时候创建 SQL视图(如我所见 Rails 不能很好地,我必须创建一个迁移)或创建任何单独的类

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

这是我目前的例子:

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


推荐答案

使用 find_by_sql

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

这篇关于更好的方式写在轨道模型内的大型SQL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-14 22:39