Ruby on Rails - how to reload classes? - ruby-on-rails

I'm developing a Ruby on Rails app, and everytime I made changes to my class file, I need to restart the server in order for the code changes to be reflected. The code is in my controllers directory, but it's not a controller.
What change do I need to make to make the class reload automatically everytime I make a change? I've set caching to false in my environment config file, and it still doesn't work.
Any ideas?

I would probably move the code out of the controllers directory (if it isn't a controller it does not belong there) to maybe lib/controller_extensions/ and add this line to my config/application.rb (rails3) or to config/environment.rb (rails 2.3.10)
config.autoload_paths += Dir["#{config.root}/lib/controller_extensions/"]

It really depends on where the classes are originally loaded. Here is a method of reloading what you want in different environments.
Why does code need to be reloaded in Rails 3?

If its the development environment I don't think you have to change the server in order to get the changes made in the controller .Rather If there are any changes made in the Model class then you have to restart the server again.

Related

Rails controller handling discrepancy development/production

Summary: in development, requests are routed to workouts_controller.rb but in production to workouts_controllerPrev.rb
In a Rails 5.2.3 app, I have the file workouts_controller.rb in the controllers/ folder with first line:
WorkoutsController < ApplicationController
I took a copy of workouts_controller.rb (to serve as a quick reference backup) which I renamed to workouts_controllerPrev.rb and retained in the controllers/ folder.
I then introduced some new functionality to workouts_controller.rb. I tested the new functionality locally (it worked as expected in development) and then I deployed to Heroku (v 7.42.0) (for production).
The new functionality, however didn’t work in production. After some debugging, I identified that in production, the WorkoutsController class in workouts_controllerPrev.rb was handling calls to the Workouts controller (rather than the Workouts controller defined in workouts_controller.rb (as anticipated and as happening in development).
I made a more dramatic name change to workouts_controllerPrev.rb, changing it to Xwurkouts_controllerPrev.rb, and changed the class name in this file to XWurkoutsController redeployed and it all worked fine.
What is happening here? Why would Rails function differently in this respect between the 2 environments? Is this a bug or an unsurprising consequence of a bad practice of having unused files loitering around? If a bug, where should I report it?
I am using SQLite in development, and PostGreSQL in production, but I don’t see this can be a database issue? The production webserver is Puma.
Thanks for any guidance
Daniel
The issue is that both files have the same class name in them, so the actions (methods) in whichever one is loaded last will override the actions defined in the one loaded first.
If you want to keep both files around and not have surprising results, change the class name in the old file to something else like PrevWorkoutsController.
Or, save it in a branch in Git so it doesn't clutter your current code.
To answer about why you got different results in different environments, it is because of the difference between autoloading vs. eager loading. Rails uses autoloading in development, but it eager loads everything up front in production, then turns autoloading off.
In other words, in development, Rails will reload the class from its matching file any time that file is saved. In production, it simply loads all files up front, so whichever one it loads last will win.
You can read more here.

Helpers don't auto refresh with every request

I am new to Ruby on Rails and recently I noticed this happening.
All code that I write in a controller
eg. class Xyz < ApplicationController...
gets refreshed with every request. Any change I make in the code in this class is reflected in the next request without restarting the server.
But any code I write in a class that does not < from ApplicationController
or a class that is in the "helpers" directory does not get auto-refreshed.
Is this normal behaviour ? Because its a pain to restart the server every time.
I am using the following in development mode :
ruby 2.0.0
Rails 4.1.8
and thin server. (also happens with webrick)
The code changes to the helpers, controllers and models are picked up the application as we refresh the page but some times it behaves weirdly and we have to restart the server.
So , basically I had created a rails-api project..that did not have the "helpers" folder by default. I had created it myself(I forgot to mention that above) ... So basically adding this line:
config.autoload_paths += %W(#{config.root}/app)
to application.rb/development.rb(depending on your need), solves this issue.
you can add :
config.autoload_paths += Dir["#{config.root}/lib/**/"]
as well if you want to autoload everything in your "lib" directory.
Hope this helps the next person.

Reflect changes to environment.rb without restarting rails server

Rails server seems to need restart when I change config/environment.rb.
Is there a way to reflect the change without restarting server?
A gem for it or something like that exists?
The environment.rb file is one of the main initialiser files for starting your app and can't really be reloaded on each request because reloading that is nearly the same as restarting the whole app. I'm guessing there are some variables in there that you want to change without restarting every time. Well instead of putting them in there, you could create a .rb file in the lib directory. That code gets run when the app is started and is useful for defining custom classes, etc.
To make it automatically reload it on each request you need to use eager_load_paths in your config/application.rb. This question specifies how to do that.

Rails Application expecting me to restart my webrick server for any change in my controller

I am working on an existing Rails 2.3.x application, So I was working on it and it was a messy code with great difficulty I was able to run the application. But now for every small change in one of my controller it is expecting me to restart my serer otherwise the changes are not reflecting back, Let's take an example scenario here, Let's just say in one of the before_filter method I just added a puts statement on top of the method and it was not printing in the log, after I restart the server it is printing, Can some one please let me know if I am missing something here.
What environment are you using?
The default environment is 'development', where the code is reloaded on each request. However, this behaviour can be overwritten in the config file.
To be sure that the code is reloaded, add this into your config/development.rb file:
# In the development environment your application's code is reloaded on
# every request. This slows down response time but is perfect for development
# since you don't have to restart the webserver when you make code changes.
config.cache_classes = false
If you are using config.threadsafe! It needs to be present before config.cache_classes=false as threadsafe makes cache_classes true again, following link would make it more clear. here
Maybe you have don't flush. The log system in Rails use a BufferedLogger. So need a flush to be print. Try a default logger.

Is environment.rb invoked on every http request?

I'm wondering what file I should use to ensure my initializing code will only be executed once when the application starts up. Is environment.rb the correct file to use or will it be invoked on every http request?
environment.rb is only loaded when the application is first started up. subsequent changes to the environment.rb file require a restart. What kind of code do you only want to execute once?
You might want to read through the Ruby on Rails guide for Configuring Rails Applications which talks about the different places to put initialization code.
Look at config/initializers for the recommended location custom startup code.
As far as possible leave environment.rb alone unless you're explicitly adding or changing items defined within the Rails::Initializer.run block.
If you want to manage custom settings across your various environments, e.g. you want production and development to have different settings for something, then the config/environments directory should be your first port of call.

Resources