arel icon indicating copy to clipboard operation
arel copied to clipboard

Alias working for where

Open daniel2d2art opened this issue 14 years ago • 1 comments

Setting alias for table:

    a = Foo.arel_table
    a1 = a.alias("#{a.name}_1")
    a2 = a.alias("#{a.name}_2")

    inner_where = a1[:attr1].eq(a2[:attr1])

just working on joins:

a.join(a2).on(a1[:attr1].eq(a2[:attr1])).to_sql

returns:

"SELECT FROM \"foos\" 
  INNER JOIN \"foos\" \"foos_2\" 
          ON \"foos\".\"attr1\" = \"foos_2\".\"attr1\""

Same approach for nested where, no way:

a.where( a.where(inner_where).exists).to_sql

Don't express Alias for From clauses

    "SELECT FROM \"foos\"  
     WHERE EXISTS (SELECT FROM \"foos\"  
                   WHERE \"foos_1\".\"attr1\" = \"foos_2\".\"attr1\")"

Self subqueries needs some like:

        "SELECT FROM \"foos\" \"foos_1\" 
     WHERE EXISTS (SELECT FROM \"foos\" \"foos_2\"  
                   WHERE \"foos_1\".\"attr1\" = \"foos_2\".\"attr1\")"

daniel2d2art avatar Feb 20 '12 23:02 daniel2d2art

Per #523, Arel development is moving to rails/rails.

If this issue is still relevant, please consider reopening it over there. (Note that the Rails repository does not accept feature request issues, and requires reproduction steps that rely on Active Record's documented API.)

matthewd avatar Apr 25 '18 08:04 matthewd