Created
June 7, 2013 07:14
-
-
Save joshuap/5727554 to your computer and use it in GitHub Desktop.
Adding #with to ActiveRecord 3.2.13
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
that's quite neat, why don't you create the pg-rails gem?