【问题标题】:Ruby: Ideas to convert Arel.sql to Arel::Nodes::Case?Ruby:将 Arel.sql 转换为 Arel::Nodes::Case 的想法?
【发布时间】:2020-12-28 11:56:06
【问题描述】:

我正在尝试重构一段代码(也为了提高我对Arel::Nodes::Case 的理解),我想转换这个Arel.sql 语句

    Arel.sql(default_order)
    def default_order
        "CASE
          WHEN applications.status = 'new' AND owner_id IS NULL THEN '1'
          WHEN applications.is_priority = '1' AND is_read = '0' AND owner_id = '#{current_user.id}'  THEN '2'
          WHEN applications.is_priority = '1' THEN '3'
          ELSE '4'
         END, applications.code"
    end

进入Arel::Nodes::Case,但我没有得到任何好的结果。有什么可以提供帮助的想法吗?

非常感谢。

更新

我采用了这种方法,但我一点也不喜欢它。太难阅读和理解:

  def default_order
    arel_applications = Application.arel_table
    Arel::Nodes::Case.new
        .when((arel_applications[:status].eq('new'))
                  .and(arel_applications[:owner_id].eq(nil)), 1)
        .when((arel_applications[:is_priority].eq(1))
                  .and(arel_applications[:is_read].eq(0))
                  .and(arel_applications[:owner_id].eq(current_user.id)), 2)
        .when(arel_applications[:is_priority].eq(1), 3)
        .else(4).to_sql
  end

即便如此,我不知道如何获取在 SQL 中用于对记录进行排序的 applications.code

【问题讨论】:

  • 我想这回答了你的问题stackoverflow.com/a/60413416/493321
  • 谢谢 Anuj,我尝试了类似的东西,但我不喜欢我得到的东西。我将用我得到的方法更新我的帖子。读起来太难太丑了

标签: ruby-on-rails ruby arel


【解决方案1】:

你不能总是期望Arel 很漂亮。 Arel 是一个非常复杂和灵活的查询组装器,这种复杂性的副作用通常是冗长。

在我看来,使用Arel 而不是原始 sql 总是优先考虑的,因为它是卫生的、动态的并且与数据库无关。

我们可以通过使用then 方法而不是when 的第二个参数来更改您的更新,使其更具可读性。这将导致代码读起来更像是 CASE 语句,例如 .when(condition).then(value)

更改将如下所示:

  def default_order
    arel_applications = Application.arel_table
    case_stmnt = Arel::Nodes::Case.new
        .when(arel_applications[:status].eq('new').and(
                 arel_applications[:owner_id].eq(nil)
             )
          ).then(1)
        .when(arel_applications[:is_priority].eq(1).and(
                arel_applications[:is_read].eq(0).and(
                  arel_applications[:owner_id].eq(current_user.id)
                )
              )
          ).then(2)
        .when(arel_applications[:is_priority].eq(1)
          ).then(3)
        .else(4)
  end

现在来处理问题的第二部分“即便如此,我也不确定如何获取在 SQL 中用于对记录进行排序的 applications.code。” p>

我们可以使用Arel::Nodes::Grouping 来处理这个问题:

def default_order
    arel_applications = Application.arel_table
    case_stmnt = Arel::Nodes::Case.new
        .when(arel_applications[:status].eq('new').and(
                 arel_applications[:owner_id].eq(nil)
             )
           ).then(1).
        .when(arel_applications[:is_priority].eq(1).and(
                arel_applications[:is_read].eq(0).and(
                  arel_applications[:owner_id].eq(current_user.id)
                )
              )
          ).then(2)
        .when(arel_applications[:is_priority].eq(1)
          ).then(3)
        .else(4)
    Arel::Nodes::Grouping.new(case_stmnt,arel_applications[:code])
  end

这将导致以下 SQL:

(CASE 
  WHEN [applications].[status] = N'new' AND [applications].[owner_id] IS NULL THEN 1 
  WHEN [applications].[is_priority] = 1 AND [applications].[is_read] = 0 AND [applications].[owner_id] = 1 THEN 2 
  WHEN [applications].[is_priority] = 1 THEN 3 
  ELSE 4 END, 
[applications].[code])

然后您可以将其用作order 的参数(无需转换to_sql,因为rails 会为您处理此问题,例如Application.order(default_order)

您还可以将排序方向附加到 case_stmntarel_applications[:code] 列,例如case_stmnt.asccase_stmnt.desc

【讨论】:

  • 非常感谢@engineersmnky,非常清楚。
猜你喜欢
  • 2013-09-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-16
  • 1970-01-01
  • 1970-01-01
  • 2012-11-05
相关资源
最近更新 更多