代码之家  ›  专栏  ›  技术社区  ›  Tintin81

如何用Arel语法重新编写我的Ruby方法?

  •  0
  • Tintin81  · 技术社区  · 7 年前

    在Rails 6.0中不允许。此方法不应与一起调用

    我知道正在讨论此弃用消息 here .

    但我仍然在努力学习正确的语法。

    这是我的方法:

    def optimized_sort_column
      column_type == :string ? "LOWER(#{unique_sort_column})" : unique_sort_column
    end
    

    我已尝试将其更改为:

    def optimized_sort_column
      column_type == :string ? Arel.sql("lower(#{unique_sort_column})") : unique_sort_column
    end
    

    但我仍然收到同样的反对警告。

    我错过了什么?

    1 回复  |  直到 7 年前
        1
  •  1
  •   Jay Dorsey    7 年前

    你能试试这个吗

    .order(
      Arel::Nodes::NamedFunction.new('lower', [unique_sort_column])
    )
    

    scuttle.io lower(author) 并用替换变量名代替作者在本地进行测试。它不会发出警告。