-
-
Save lukeredpath/9d7f86e27014ef5df280 to your computer and use it in GitHub Desktop.
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
module RailsExtensions | |
module HasManyThroughWithScope | |
def has_many_with_scope(association, options={}) | |
if (through = options[:through]) && (scope = options.delete(:scope)) | |
if conditions = through.to_s.classify.constantize.send(scope).proxy_options[:conditions] | |
scope_conditions = conditions.inject({}) do |scope_conditions, (key, value)| | |
scope_conditions["#{through}.#{key}"] = value | |
scope_conditions | |
end | |
options[:conditions] = (options[:conditions] || {}).merge(scope_conditions) | |
end | |
end | |
has_many_without_scope(association, options) | |
end | |
end | |
end | |
ActiveRecord::Base.extend(RailsExtensions::HasManyThroughWithScope) | |
ActiveRecord::Base.metaclass.instance_eval do | |
alias_method_chain :has_many, :scope | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment