I am using the Pry gem in my Rails console, but the pry flavored rails-console seems to have lost the reload! method for reloading models and stuff.
Here's how I start the pry console
c:\rails\app> pry -r ./config/environment
Thank You
For anyone coming to this question recently: the answer has changed in Rails 3.2, because they've changed how they implement reload!
Where in earlier version the irb commands were added as methods to Object
, now they are added to IRB::ExtendCommandBundle
to avoid polluting the global namespace.
What I do now is (1) in development.rb
silence_warnings do
begin
require 'pry'
IRB = Pry
module Pry::RailsCommands ;end
IRB::ExtendCommandBundle = Pry::RailsCommands
rescue LoadError
end
end
and (2) in .pryrc
if Kernel.const_defined?("Rails") then
require File.join(Rails.root,"config","environment")
require 'rails/console/app'
require 'rails/console/helpers'
Pry::RailsCommands.instance_methods.each do |name|
Pry::Commands.command name.to_s do
Class.new.extend(Pry::RailsCommands).send(name)
end
end
end
Here's the link to the Rails pull request where the change was introduced - https://github.com/rails/rails/pull/3509
You could check out this page on the Pry wiki: https://github.com/pry/pry/wiki/Setting-up-Rails-or-Heroku-to-use-Pry
Also check out the pry-rails
plugin: https://github.com/rweng/pry-rails
There's also a lot of other content on that wiki, it's a great resource.
You could tell Pry to load your Rails environment in your .pryrc
rails = File.join Dir.getwd, 'config', 'environment.rb'
if File.exist?(rails) && ENV['SKIP_RAILS'].nil?
require rails
if Rails.version[0..0] == "2"
require 'console_app'
require 'console_with_helpers'
elsif Rails.version[0..0] == "3"
require 'rails/console/app'
require 'rails/console/helpers'
else
warn "[WARN] cannot load Rails console commands (Not on Rails2 or Rails3?)"
end
end
This will give your reload!
back.
If you are having trouble with Zeus and Pry, try adding to your .pryrc
:
if Kernel.const_defined?(:Rails) && Rails.env
require File.join(Rails.root,"config","environment")
require 'rails/console/app'
require 'rails/console/helpers'
extend Rails::ConsoleMethods
end
Taken from here
I've recently written a post about pry and rails. You can find it here http://lucapette.com/pry/pry-everywhere/. By the way, as dave already said, you would like to use pry with:
pry -r ./config/environment
I recommend you to try what I wrote in the article, it works really fine.
A better version of @Rodrigo Dias's answer.
If you don't want to use pry-rails
gem then just add following into your .pryrc
-
if defined?(Rails) && Rails.env
if defined?(Rails::ConsoleMethods)
include Rails::ConsoleMethods
else
def reload!(print=true)
puts "Reloading..." if print
ActionDispatch::Reloader.cleanup!
ActionDispatch::Reloader.prepare!
true
end
end
end
This code properly identifies environments and doesn't blindly includes Rails::ConsoleMethods
.
Source - Github thread comment