Paul Biggar
Paul Biggar

Reputation: 28739

How do I call the Rails console's reload! command programmatically?

When using the Rails console, there's a handy reload! function which reloads models and such. How do I call this from another part of my program?

Edit I've been asked for the use case a bit. It was that I had a long running JVM process running Jruby and a clojure repl. I could run RSpec tests from the REPL and could run arbitrary Ruby code from there too. But I couldn't figure out how to reload the Ruby classes so that I could edit the code and see it changed in the Ruby runtime. I no longer use this setup, principally because testing it was such a pain.

(I'm using Jruby and can access the Ruby VM programatically from my backend).

Upvotes: 2

Views: 340

Answers (1)

Aaron Breckenridge
Aaron Breckenridge

Reputation: 1819

Have you tried touching restart.txt? Unfortunately, I have no experience with JRuby, but confirmed it works on my app.

FileUtils.touch('tmp/restart.txt')

You probably want to do something other than a Get request, and secure it behind some authentication.

I threw it in an Admin controller and added the route to config/routes.

# app/controllers/admin.rb
class AdminController < ApplicationController::Base
    @@time = Time.now # This value gets cached with the model.
    def reboot
        FileUtils.touch('tmp/restart.txt')
        @restarted_time = @@time
    end
end

# config/routes.rb
namespace :admin
    get 'reboot'
end

# app/views/admin/reboot.html.erb
<%= @restarted_time.to_s %>

Upvotes: 2

Related Questions