Skip to content

Instantly share code, notes, and snippets.

@elgalu
Last active December 19, 2015 07:59
Show Gist options
  • Save elgalu/5922818 to your computer and use it in GitHub Desktop.
Save elgalu/5922818 to your computer and use it in GitHub Desktop.
Add helper methods to your Rails 3 console [from blog](http://opensoul.org/blog/archives/2012/11/08/add-helper-methods-to-your-rails-console)
# config/application.rb
module SpeakerDeck
class Application < Rails::Application
# …
console do
require 'speaker_deck/console'
Rails::ConsoleMethods.send :include, SpeakerDeck::Console
end
end
end
# lib/speaker_deck/console.rb
module SpeakerDeck
# These methods are included into the Rails console
module Console
# Find a user by username
def u(username)
User.find_by_username!(username)
end
# Find a talk by url, e.g. "holman/how-to-be-sexy-like-me"
def t(param)
username, slug = param.split('/')
Talk.find_by_username_and_slug!(username, slug)
end
end
end
>> user = u 'bkeepers'
=> #<User id:…>
>> talk = t 'sachag/side-projects'
=> #<Talk id:…>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment