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
  • Save artificemm/ffb1220da8c49c1cbcdceefceae44e7b to your computer and use it in GitHub Desktop.
Save artificemm/ffb1220da8c49c1cbcdceefceae44e7b to your computer and use it in GitHub Desktop.
# 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})"))])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment