Created
May 11, 2012 16:09
-
-
Save JeffCohen/2660678 to your computer and use it in GitHub Desktop.
Fun with Ruby classes
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
# Open up a Rails app that has at least one model | |
# Create a file named config/initializers/sample.rb | |
# Paste this code into it | |
# Open up your console | |
# Try to figure out how to call these methods | |
# Try to figure out how they work | |
class ActiveRecord::Base | |
def self.sample | |
offset(rand(0...count)).first | |
end | |
def self.[](id) | |
find_by_id(id) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment