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:
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
What Is the Use of Gemfile in Rails
Why Use "Self" to Access Activerecord/Rails Model Properties
How to Check If a Ruby Object Is a Boolean
Include Jekyll/Liquid Template Data in a Yaml Variable
Storing Objects in a Session in Rails
Returning All Maximum or Minimum Values That Can Be Multiple
Can't Install Rails on MAC Os Catalina
Converting Ruby Array to Array of Consecutive Pairs
Passing Block into a Method - Ruby
Uninstalling All Gems Ruby 2.0.0
How to Search a Folder and All of Its Subfolders For Files of a Certain Type
Uploading Multiple Files With Paperclip
How to Create a Sha1 Hash in Ruby
Rails Each Loop Insert Tag Every 6 Items
Ruby: How to Copy a Variable Without Pointing to the Same Object
Error: Mime-Types-Data Requires Ruby Version >= 2.0
Is There a Hook Similar to Class#Inherited That's Triggered Only After a Ruby Class Definition