Skip to content

Instantly share code, notes, and snippets.

@joshed-io
Created July 17, 2012 08:10
Show Gist options
  • Save joshed-io/3127926 to your computer and use it in GitHub Desktop.
Save joshed-io/3127926 to your computer and use it in GitHub Desktop.
Load users in Rails console by username via method_missing
#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"
@joshed-io
Copy link
Author

And for a username with the first letter capitalized:

send "Dzello"

to avoid uninitialized constant errors.

@dorkitude
Copy link

This is cool :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment