Created
August 9, 2012 17:01
-
-
Save stevenbristol/3305917 to your computer and use it in GitHub Desktop.
A class with a proc scope
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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