Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why will Rails Console not load Changes to ruby file

I am writing a script that I run in the rails console.

When I make a change to the file, even if I exit and reload the rails console, it does not recognize these changes.

How do I make rails recognize and load changes to this file?

The file is in the root of my application. I load the rails console and run:

require 'file.rb'

The file will run fine the first time, but if I make changes to the file they are not loaded. I could delete all the code in the file and run the file again and it still runs as it did before the edits.

like image 596
Davey Avatar asked Dec 16 '22 05:12

Davey


1 Answers

If you're making changes to files that are stored within the app/ directory, you can reload these with the reload! command.

If you're making changes outside of that, you need to exit and restart your console.

As a note, the .rb extension is almost always omitted in a require call. Another thing to keep in mind is that require 'file' will look in your $LOAD_PATH for something called file.rb and will load in the first one. It's possible you have two files with the same name. Try renaming it to see if that has any effect.

Rails has a pretty good auto-loader so it's usually not necessary to use require if you put your files in the correct place to start with. For example, MyClass could go in app/models/my_class.rb and would load automatically. The autoloader can be configured to look in different places as well.

like image 169
tadman Avatar answered Jan 05 '23 20:01

tadman