Skip to content

Instantly share code, notes, and snippets.

@artificemm
Forked from midhunkrishna/arel_multiple_cte.rb
Created June 19, 2021 23:40
Show Gist options
  • Select an option

  • Save artificemm/ffb1220da8c49c1cbcdceefceae44e7b to your computer and use it in GitHub Desktop.

Select an option

Save artificemm/ffb1220da8c49c1cbcdceefceae44e7b to your computer and use it in GitHub Desktop.

Revisions

  1. @midhunkrishna midhunkrishna created this gist Jul 14, 2017.
    20 changes: 20 additions & 0 deletions arel_multiple_cte.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    # from_cte_sql = ::Namespace::From.where_clause
    # ..


    from_table = Arel::Table.new(:from_lane)
    to_table = Arel::Table.new(:to_lane)
    rates_table = Arel::Table.new(:rates)

    rates_table
    .join(from_table).on(rates_table[:from_id].eq(from_table[:id]))
    .join(to_table).on(rates_table[:to_id].eq(to_table[:id]))
    .project(rates_table[Arel.star])
    .with([
    Arel::Nodes::As.new(from_table,
    Arel::Nodes::SqlLiteral.new("(#{from_cte_sql.to_sql})")),
    Arel::Nodes::As.new(to_table,
    Arel::Nodes::SqlLiteral.new("(#{to_cte_sql.to_sql})")),
    Arel::Nodes::As.new(rates_table,
    Arel::Nodes::SqlLiteral.new("(#{rate_cte_sql.to_sql})"))])