Rails Console: Reload! Not Reflecting Changes in Model Files? What Could Be Possible Reason

Rails Console: reload! not reflecting changes in model files? What could be possible reason?

reload! only reloads the latest code in the console environment. It does not re-initialize existing objects.

This means if you have already instantiated any objects, their attributes would not be updated - including newly introduced validations. However, if you create a new object, its attributes (and also validations) will reflect the reloaded code.
more here

Helper method doesn't reload by `reload!` in rails console

You are correct that helper represents an object that is already instantiated, and therefore not affected by calls to reload!. The helper method in the console is defined as:

def helper
@helper ||= ApplicationController.helpers
end

The first time you call helper, it will memoize the ApplicationController helpers. When you call reload!, the ApplicationController class (and it's helpers) are reloaded, but the helper method is still looking at an old instance.

Instead of using the helper method, you can call ApplicationController.helpers directly, and you will see your changes after running reload!:

> helper.foo
# => "foo"
> ApplicationController.helpers.foo
# => "foo"
> # Change the return value of PostsHelper from "foo" to "bar"
> reload!
> helper.foo
# => "foo"
> ApplicationController.helpers.foo
# => "bar"

EDIT

Starting with Rails 5, this will no longer be an issue. A PR was merged to remove memoization from the helper console method.

Rails server doesn't see code changes and reload files

I've solved my problem adding below line to the development.rb file.

config.reload_classes_only_on_change = false

Reload rails console with hotkey

In iTerm2 you can do smthng like:

Preferences -> Keys -> Global Shortcut Keys -> +

And add following: Sample Image

Rails console in development doesn't load the classes Rails 3.2

You need to use "reload!" for changes in class when in Rails console.

config.cache_classes = false

means class files will be reload every request, not on Rails console.



Related Topics



Leave a reply



Submit