Skip to content

Instantly share code, notes, and snippets.

@stevenbristol
Created August 9, 2012 17:01
Show Gist options
  • Save stevenbristol/3305917 to your computer and use it in GitHub Desktop.
Save stevenbristol/3305917 to your computer and use it in GitHub Desktop.
A class with a proc scope
class User < ActiveRecord::Base
scope :hello_lamb, ->(param) { param.blank? ? where("") : where("param = ?", param) }
scope :hello_proc, Proc.new { |param| param.blank? ? where("") : where("param = ?", param) }
end
User.hello_lamb "a"
# User Load (0.5ms) SELECT * FROM users WHERE (param = 'a')
#=> #<ActiveRecord::Relation:0x3fe6eedeb984>
User.hello_proc "a"
# User Load (0.5ms) SELECT * FROM users WHERE (param = 'a')
#=> #<ActiveRecord::Relation:0x3fe6eedeb984>
User.hello_lamb ""
# User Load (0.5ms) SELECT * FROM users
#=> #<ActiveRecord::Relation:0x3fe6eedeb984>
User.hello_proc ""
# User Load (0.5ms) SELECT * FROM users
#=> #<ActiveRecord::Relation:0x3fe6eedeb984>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment