Skip to content

Instantly share code, notes, and snippets.

@keeran
Created September 14, 2010 13:13
Show Gist options
  • Select an option

  • Save keeran/579005 to your computer and use it in GitHub Desktop.

Select an option

Save keeran/579005 to your computer and use it in GitHub Desktop.
class Posts < ActiveRecord::Base
belongs_to :user
scope :extra, where(:extra => true)
def self.optional_scope(param = false)
if param
scope = where(:something => 'else')
else
scope = self
end
scope
end
end
# working use
current_user.posts.optional_scope(true).extra.each { .. } current_user.posts.optional_scope(false).extra.each { .. }
# falling over because Class is returned
current_user.posts.optional_scope(false).each { .. }
# really ought to test that it's still maintaining the prior scopes in the chain...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment