Rails: How to make require reload on every refresh? - ruby-on-rails

I've notice that when I include password hashing with require "digest/sha1" on top of my model, every time I make some change in my app, I have to restart server to see changes.
That is kind of annoying and it is slowing down development a lot, especially for beginner like me.
I've seen somewhere that with require_dependency "digest/sha1" it should work, but it is not working for me (saying no file error).
I'm not sure even where to put require_dependency as I haven't found any example.
There should be some way to make it work, as it is quite common problem, maybe I was just looking to wrong places.
Thanks
EDIT :
I've came to conclusion that error comes when my Ubuntu machine goes to sleep. After wake up, local server (tried thin and WEBrick) give that error.
EDIT :
It has nothing to do with Ubuntu sleep. I had a function named hash that was giving errors every time, complaining that it got wrong number of arguments, but that function was never called.
So, I've renamed it to encrypt and now it is working, but I'm not 100% sure that it is solution, I have to test is more.
If it will be ok, that would be a strange bug, I will post an answer.

In development mode, by default, anything in app/ or config/routes.rb is reloaded between requests. If this is not happening for you, it's probably not caused by the require. It's more likely that you've turned off reloading inadvertently.
I've used digest/sha1 in many projects before and have never had this problem, usually including it in the User model where it's used.
Can you replicate this problem in a brand new Rails project? Does the problem go away if you remove that line? If so that is very odd.

If you remove require "digest/sha1" all works fine?
Anyway check for config.cache_classes = false in config/environments/development.rb

Related

Rails: How to autoload constants successfully on first attempt without proper namespacing?

I want the following file structure:
app/components/foo/foo.rb
app/components/foo/_foo.html.haml
app/components/bar/bar.rb
app/components/bar/_bar.html.haml
Where I then can access the different classes like this:
Components::Foo
Components::Bar
Right now, for autoloading to work properly, it has to be structured/coded/accessed like this (hypothetically according to Rails convention):
Components::Foo::Foo
Components::Bar::Bar
That makes no sense. I want the previously mentioned file structure because I want my classes to be self-contained together with any resources/etc. belonging to them. I know that if the ruby file goes right inside app/components all will be well, but that means I have one folder and one file to keep track of, instead of just one folder. Not very nice.
Now, what's bothering me is that, just like the scenario presented here: Rails unable to autoload constant only on first attempt, the autoloading clearly works, but always only on the 2nd attempt. Since it is so systematic, it feels like it should be possible to configure things correct for it to work on the 1st attempt at all times.
This is the only addition I've made to the application.rb:
config.eager_load_paths += Dir[Rails.root.join('app', 'components', '**/')]
I'm aware that a server restart is necessary in case new directories are added/existing directory structure is modified, but that's fine since that doesn't happen very often. Modifying the code obviously happens very often, and while I can keep refreshing the page multiple times until it has re-loaded all the constants, it is very frustrating (and tbh I haven't even tested it in production, but wouldn't be surprised if it does the same thing there on the first load).
I'm also aware that you might be able to work around this by using manual requires etc., and while I haven't had much luck with that anyway, it is still a much inferior solution since it would require a server restart if code is modified (assuming the requiring is done in a central place, such as in an initializer or in application.rb).

How can I find out what prevents a rails app from loading a page?

I have a rails which seemed to be working previously but now after some changes when I go to the root page it takes it infinitely to load it, it just doesn't load it. There're nothing useful in the console either. How can I find out what prevents it from loading the main page? Is it about profiling?
Check your Rails logs, eg. development.rb. You can put logger.info, or puts statements in your environment.rb, development.rb and application.rb files to see how far Rails is getting in the boot process. You can also create a dumb initializer named 00_start_init.rb with a logger.info or puts statement to see if you're getting as far as initialization. I've found that useful before.
To really understand where you application is hanging, you need to understand the Rails initialization process. Here is the documentation for Rails version 4.2. http://guides.rubyonrails.org/v4.2/initialization.html. Similar documentation exists for every version of Rails. You can take advantage of understanding the boot sequence by placing log statement at various point in the process.
I'm assuming you're in the development environment. If so, and the console loads, it's likely not a configuration problem. It's more likely a problem with your controllers or models. If the console won't load to a prompt, then it's likely a configuration problem in application.rb, development.rb, an initializer, etc.
You mention profiling, but provide no details about it. I can't even guess what you're referring to, so the answer is "maybe?". If you can post the code changes you made since the app last loaded in the browser, that would make it much easier to help you trouble-shoot.

Models not reloading in development in Rails (3.2.11) project

I've searched fairly extensively for any advice and have yet to find it so, here goes:
My Rails project fails to automatically reload models in development. Reloading them currently requires a full server restart.
Previous instances of this issue have been related to non-activerecord files placed in the models directory, though this is not the case for me.
config.cache_classes is properly set to false in my development config file. Views and controllers reload without issue.
All of my rails components are version 3.2.11. I have tried disabling all of my development-specific gems to no avail. This is obviously not a productivity stopper, but it is quite an annoyance. Any help appreciated and I am happy to provide more information if it would help, though I am not using any exotic gems.
Thanks!
Some possibilities:
You are not really running on developement environment
You are changing a model within a namespace and didn't told rails to autoload the path
You are changing a file that is included in your class, not your class directly (or any of the many variants for this)
You are caching classes
Considerations:
Things might change according to the webserver you are using
How do you know it's not reloading?
I ask my question because I was having the exact same issue when I was trying to insert a debugger into what I thought was a piece of code that was being executed. I assumed the model wasn't being reloaded since it was not hitting the debugger but it was actually a call back that was redirecting me around the code with the debugger line in it.
So, it might be something other than your models not being reloaded.

why might rails-2.3.5 not reload some files under app in development mode?

I've added a subdirectory app/renderers after Railscast #101. The classes in that directory are not getting reloaded by my development server. It's driving me a little bonkers.
I've read everything I could find on forcing it to reload lib and/or plugins but this seems to be a different case since "everything under app should be reloaded automatically." Plus, I've checked ActiveSupport::Dependencies.load_once_paths, and app/renderers definitely isn't in it.
I'd also like to get the renderers to be automatically required, so that I don't have to go around putting require statements in the rest of my code. Is that sensible? How does it work for, say, models and other constants?
Doh. I should have been loading the files, not requiring them.
I'd still like to have them magically loaded -- not needing a specific load statement for each one -- but for now it's working :)

Automatically reload rails module

I'm developing a ruby module that I include in my rails app. I want it to be reloaded automatically when it changes. I've done extensive googling, and looked at the various questions here that discuss it, but they all seem out of date or wrong.
How do I get an external module to be reloaded in rails when it changes? I've tried adding its name to ActiveSupport::Dependencies.unloadable_constants, but after I type reload! in the console, I can no longer refer to that symbol without a NameError: uninitialized constant foo, even if I do another require 'foo_module'. Does anyone know how to get this working?
Note: here is one possible dup, but note in the comments to the 'answer' that it never solved the problem for modules. There's also this question which has a dead link in the answer, and finally this one, which also doesn't solve it.
I found how to do this:
Make sure FooModule is in lib/foo_module.rb.
Use require_dependency to require your external library in lib/foo_module.rb.
These steps are both required, and no others are required.
There are two separate problems here.
The simpler of which is that you are using require, when you want load.
require will evaluate the code in a file once, no matter how many times that file/module is required.
load will evaluate the code in a file each time that file is loaded.
require is preferred to load used so that the files are not evaluated multiple times when many files depend on them.
The short version is that load can be used to reload modules that have already been loaded by require.
The more complicated problem is automatically reloading a module on change.
One of the possible duplicates listed in the question links to here. Which suggests prefixing any code that depends on your module with a conditional load of your module if it has changed since loading. You'll need to use a global variable to keep track of when a file was loaded.
N.B.: this should not be used on a production server, but should be fine on a development server or console.
I spent sometimes to research this issue as well.
Here's my findings on how to auto-reload require files in Rails without restarting server.
The solution is now available as a Ruby gem require_reloader.

Resources