Skip to content

Instantly share code, notes, and snippets.

@mildmojo
Last active April 5, 2024 16:00
Show Gist options
  • Select an option

  • Save mildmojo/3724189 to your computer and use it in GitHub Desktop.

Select an option

Save mildmojo/3724189 to your computer and use it in GitHub Desktop.

Revisions

  1. mildmojo revised this gist Sep 28, 2017. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions left_join_arel_example.rb
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,10 @@
    # Here's a contrived example of a LEFT JOIN using ARel. This is an example of
    # the mechanics, not a real-world use case.

    # NOTE: In the gist comments, @ozydingo linked their general-purpose ActiveRecord
    # extension that works for any named association. That's what I really wanted!
    # Go use that! Go: https://gist.github.com/ozydingo/70de96ad57ab69003446

    # == DEFINITIONS
    # - A Taxi is a car for hire. A taxi has_many :passengers.
    # - A Passenger records one person riding in one taxi one time. It belongs_to :taxi.
  2. mildmojo revised this gist Jan 18, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion left_join_arel_example.rb
    Original file line number Diff line number Diff line change
    @@ -9,7 +9,7 @@ class Taxi < ActiveRecord::Base
    # This scope LEFT JOINs the Passenger table. You might use this if you wanted
    # to select taxis by a set of taxi and passenger IDs (as with the
    # SQL "taxis.id IN () OR passengers.id IN ()").
    def left_join_passengers
    def self.left_join_passengers
    taxis = Taxi.arel_table
    passengers = Passenger.arel_table

  3. mildmojo revised this gist Aug 31, 2015. No changes.
  4. mildmojo revised this gist Oct 3, 2012. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion left_join_arel_example.rb
    Original file line number Diff line number Diff line change
    @@ -22,4 +22,7 @@ def left_join_passengers
    join_sources
    joins(taxi_passengers)
    end
    end
    end

    # Sources that almost documented this:
    # http://blog.donwilson.net/2011/11/constructing-a-less-than-simple-query-with-rails-and-arel/
  5. mildmojo revised this gist Sep 17, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion left_join_arel_example.rb
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@
    # the mechanics, not a real-world use case.

    # == DEFINITIONS
    # - A Taxi is a car for hire. A taxi has_many :passengers, class_name: 'Passenger'.
    # - A Taxi is a car for hire. A taxi has_many :passengers.
    # - A Passenger records one person riding in one taxi one time. It belongs_to :taxi.

    class Taxi < ActiveRecord::Base
  6. mildmojo renamed this gist Sep 14, 2012. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  7. mildmojo revised this gist Sep 14, 2012. 1 changed file with 9 additions and 9 deletions.
    18 changes: 9 additions & 9 deletions gistfile1.rb
    Original file line number Diff line number Diff line change
    @@ -2,24 +2,24 @@
    # the mechanics, not a real-world use case.

    # == DEFINITIONS
    # - A Taxi is a car for hire. A taxi has_many :riders, class_name: 'RiderHistory'.
    # - A Taxi is a car for hire. A taxi has_many :passengers, class_name: 'Passenger'.
    # - A Passenger records one person riding in one taxi one time. It belongs_to :taxi.

    class Taxi < ActiveRecord::Base
    # This scope LEFT JOINs the RiderHistory table. You might use this if
    # you wanted to select taxis by a set of taxi and passenger IDs (as with the
    # This scope LEFT JOINs the Passenger table. You might use this if you wanted
    # to select taxis by a set of taxi and passenger IDs (as with the
    # SQL "taxis.id IN () OR passengers.id IN ()").
    def left_join_riders
    def left_join_passengers
    taxis = Taxi.arel_table
    passengers = Passengers.arel_table
    passengers = Passenger.arel_table

    # The key here is providing the join type (Arel::Nodes::OuterJoin) and
    # grabbing the ARel join object itself from Arel::SelectManager#join_sources
    # to feed to ActiveRecord's #joins method.
    # SQL equivalent: "LEFT OUTER JOIN passengers ON taxis.id = passengers.taxi_id"
    taxi_riders = taxis.join(passengers, Arel::Nodes::OuterJoin).
    on(taxis[:id].eq(passengers[:taxi_id])).
    join_sources
    joins(taxi_riders)
    taxi_passengers = taxis.join(passengers, Arel::Nodes::OuterJoin).
    on(taxis[:id].eq(passengers[:taxi_id])).
    join_sources
    joins(taxi_passengers)
    end
    end
  8. mildmojo created this gist Sep 14, 2012.
    25 changes: 25 additions & 0 deletions gistfile1.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    # Here's a contrived example of a LEFT JOIN using ARel. This is an example of
    # the mechanics, not a real-world use case.

    # == DEFINITIONS
    # - A Taxi is a car for hire. A taxi has_many :riders, class_name: 'RiderHistory'.
    # - A Passenger records one person riding in one taxi one time. It belongs_to :taxi.

    class Taxi < ActiveRecord::Base
    # This scope LEFT JOINs the RiderHistory table. You might use this if
    # you wanted to select taxis by a set of taxi and passenger IDs (as with the
    # SQL "taxis.id IN () OR passengers.id IN ()").
    def left_join_riders
    taxis = Taxi.arel_table
    passengers = Passengers.arel_table

    # The key here is providing the join type (Arel::Nodes::OuterJoin) and
    # grabbing the ARel join object itself from Arel::SelectManager#join_sources
    # to feed to ActiveRecord's #joins method.
    # SQL equivalent: "LEFT OUTER JOIN passengers ON taxis.id = passengers.taxi_id"
    taxi_riders = taxis.join(passengers, Arel::Nodes::OuterJoin).
    on(taxis[:id].eq(passengers[:taxi_id])).
    join_sources
    joins(taxi_riders)
    end
    end