Skip to content

Instantly share code, notes, and snippets.

@kirel
Created May 30, 2013 15:41
Show Gist options
  • Select an option

  • Save kirel/5678865 to your computer and use it in GitHub Desktop.

Select an option

Save kirel/5678865 to your computer and use it in GitHub Desktop.
# scope uses merge which looses conditions when chained on the same attribute
# see https://github.com/rails/rails/issues/7365 for the issue
# see https://github.com/rails/rails/blob/da5e5c5f779355a2e99e63a90612cbeaeb0fc986/activerecord/lib/active_record/relation/spawn_methods.rb#L35 for an explanation
# see https://github.com/rails/rails/commit/cd26b6ae7c1546ef8f38302661bbedf8cb487311 for a fix in Rails 4
# The workaround is to just use class methods - we wrap a dsl around just that
# Usage: extend ChainableScope in your ActiveRecord::Base derived class
module ChainableScope
def chainable_scope name
raise 'chainable_scope must be called with a block!' unless block_given?
define_singleton_method name do |*args|
yield(*args) or scoped
end
end
end
ActiveRecord::Base.extend(ChainableScope)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment