Created
July 17, 2012 08:10
-
-
Save joshed-io/3127926 to your computer and use it in GitHub Desktop.
Load users in Rails console by username via method_missing
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
#adapt to your ORM/ODM as needed | |
#put in outermost scope of a file require'd into an IRB session | |
def self.method_missing(method_name, *args, &block) | |
User.where(username: method_name).first || super | |
end | |
# usage | |
# $ rails c | |
# >> dzello.username | |
# "dzello" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
And for a username with the first letter capitalized:
send "Dzello"
to avoid uninitialized constant errors.