Skip to content

Instantly share code, notes, and snippets.

@joshuap
Created June 7, 2013 07:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshuap/5727554 to your computer and use it in GitHub Desktop.
Save joshuap/5727554 to your computer and use it in GitHub Desktop.
Adding #with to ActiveRecord 3.2.13
# config/initializers/active_record.rb
module ActiveRecordRelationExt
def self.included(base)
base.send(:attr_accessor, :with_values)
base::MULTI_VALUE_METHODS << :with
end
def with(*subqueries)
return self if subqueries.blank?
relation = clone
relation.with_values += subqueries.flatten
relation
end
def build_arel
arel = super
arel.with(*with_values) unless with_values.empty?
arel
end
end
ActiveRecord::Relation.send(:include, ActiveRecordRelationExt)
class ActiveRecord::Base
class << self
delegate :with, :to => :scoped
end
end
@thiagopintodev
Copy link

that's quite neat, why don't you create the pg-rails gem?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment