Skip to content

Instantly share code, notes, and snippets.

@lukeredpath
Created February 20, 2009 17:55
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 lukeredpath/9d7f86e27014ef5df280 to your computer and use it in GitHub Desktop.
Save lukeredpath/9d7f86e27014ef5df280 to your computer and use it in GitHub Desktop.
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