Skip to content

Instantly share code, notes, and snippets.

@samokoder
samokoder / left_join_arel_example.rb
Created February 15, 2019 11:15 — forked from mildmojo/left_join_arel_example.rb
LEFT JOIN in ARel for ActiveRecord in Ruby on Rails
# 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.
@samokoder
samokoder / active_record_extension.rb
Created February 15, 2019 11:55 — forked from ozydingo/active_record_extension.rb
ActiveRecord left join
module ActiveRecordExtension
extend ActiveSupport::Concern
module ClassMethods
# Simple left join taking advantage of existing Rails & Arel code
def left_joins(*args)
inner_joins = self.joins(*args).arel.join_sources
left_joins = inner_joins.map do |join|
Arel::Nodes::OuterJoin.new(join.left, join.right)
end
// api.js
export const getTasks() {
return get('/api/tasks')
.then(resp => new Promise(resolve => {
const { headers } = resp;
return resp.json()
.then(body => resolve({ body, headers }));
}));
};