将 CROSS JOIN 添加到 Arel

Add CROSS JOIN to Arel

我喜欢将 CROSS JOIN 添加到我的 RoR 应用程序中。使用 Arel 时,可以像这样添加连接:

cars.joins(Car.arel_table.join(Part.arel_table, Arel::Nodes::OuterJoin).on(Car.arel_table[:id].eq(Part.arel_table[:car_id])) 就我而言,我需要一个 CROSS JOIN,但它在 Arel 中不可用。如何向 Arel 添加 CROSS JOIN?

我找到了 OuterJoin class 并使用以下代码添加了一个新文件:

module Arel module Nodes class CrossJoin < Arel::Nodes::Join end end end

但这似乎还不足以使其发挥作用。我收到类型错误:无法访问 Arel::Nodes::CrossJoin

我在浏览了 Arel 资源后自己弄明白了。我需要向 Arel 添加一个 Visitor 方法以使其工作,我复制了 visit_Arel_Nodes_InnerJoin 方法并将其重命名。

module Arel
 module Nodes
   class CrossJoin < Arel::Nodes::Join
   end
 end
 module Visitors
   class ToSql
     def visit_Arel_Nodes_CrossJoin o, collector
       collector << "CROSS JOIN "
       collector = visit o.left, collector
       if o.right
         collector << SPACE
         visit(o.right, collector)
       else
         collector
       end
     end
   end
 end

结束