Skip to content

Instantly share code, notes, and snippets.

@joahking
Created November 9, 2009 11:31
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 joahking/229884 to your computer and use it in GitHub Desktop.
Save joahking/229884 to your computer and use it in GitHub Desktop.
# this is an AR model class
named_scope :privates, :conditions => { :public => false }
named_scope :named_like, lambda { |name| { :conditions => "name LIKE '#{name}%'" }}
# we want to concatenate named_scopes at later points in code
def self.by_privacy_and_name(privates = false, name = nil)
# we want a named_scope to concatenate other namescopes afterwards
gs = if privates
self.privates # with named_scopes everything runs fine
else
self.all # here we won't get a NamedScope, instead it'll be an Array
end
if name
gs.named_like name # this blows with Array returned by AR.all
else
gs
end
end
NoMethodError in 'Group by_privacy_and_name search method should return all groups starting with letter if no privacy passed'
undefined method `named_like' for #<Array:0xb5951d4c>
/home/joahking/dev/rails/tucamon/app/models/group.rb:54:in `by_privacy_and_name'
./spec/models/group_spec.rb:62:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment