rzane / baby_squeel

:pig: An expressive query DSL for Active Record
MIT License
500 stars 49 forks source link

Add .on method to Arel::Node::TableAlias for subquery alias joins #103

Open CodingAnarchy opened 5 years ago

CodingAnarchy commented 5 years ago

I was running into a situation on a production app where we were doing a join on a subquery, and we were getting an undefined method 'on' for Arel::Node::TableAlias with the following structure:

# Query 1
subquery = Foo.joins(:bar).select('DISTINCT ON (foo.id) foo_id, bar.timestamp').order('foo.id, bar.timestamp DESC')

# Query 2
Foo.distinct.joining{ |foo| subquery.as('foo_subquery').on { foo_id == foo.id }.outer }

I added this patch, and that allowed the second query to proceed and generate the correct SQL to hit the database. I thought it would be helpful to create a pull request, especially as it appears that this might be what was trying to be done in #46?

coveralls commented 5 years ago

Coverage Status

Coverage decreased (-3.9%) to 95.126% when pulling 7f5103c3f953a08f29c84768e259c8760059da36 on CodingAnarchy:table-alias-on into a71a46290f49261071031b3f5a868d574f2e96c8 on rzane:master.

coveralls commented 5 years ago

Coverage Status

Coverage decreased (-4.4%) to 94.605% when pulling 7f5103c3f953a08f29c84768e259c8760059da36 on CodingAnarchy:table-alias-on into a71a46290f49261071031b3f5a868d574f2e96c8 on rzane:master.