41

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

Nik So
  • 16,683
  • 21
  • 74
  • 108

9 Answers9

24

To use reload! like the rails console command, add this code to your .pryrc

# load Rails Console helpers like reload
require 'rails/console/app'
extend Rails::ConsoleMethods
puts 'Rails Console Helpers loaded'

EDIT== Gem pry-rails already do all of this, much simplier .

Rodrigo Dias
  • 1,331
  • 2
  • 13
  • 18
12

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

telent
  • 1,813
  • 15
  • 21
6

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.

Simon Ernst
  • 1,430
  • 9
  • 10
  • 1
    ahhhh, the reload! appears to be working but actually didn't reload any models; however, the plugin from Banister's answer seems to do it – Nik So Sep 09 '11 at 10:08
  • 2
    in Rails 3.2 I also needed to say `include Rails::ConsoleMethods` – Peter Jan 25 '12 at 23:40
6

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.

Paweł Gościcki
  • 9,066
  • 5
  • 70
  • 81
horseyguy
  • 29,455
  • 20
  • 103
  • 145
3

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

guapolo
  • 2,443
  • 2
  • 20
  • 19
2

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.

lucapette
  • 20,564
  • 6
  • 65
  • 59
2
alias pryr="pry -r ./config/environment -r rails/console/app -r rails/console/helpers"
grosser
  • 14,707
  • 7
  • 57
  • 61
1

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

Swaps
  • 1,450
  • 24
  • 31
1

Do you mean ./config/environment?

In any case, I think that's different than actually launching a rails console, which is where reload! comes from. I redefine IRB = Pry in my env-specific config file, which ensures a full console, and it all works like a charm.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302