How to reload model in rails console?

> rvm list rvm rubies =* ruby-1.9.2-p290 [ i686 ] ruby-1.9.3-p194 [ i686 ] > rails -v Rails 3.1.1 

Associated but not answered: Is there a Rails Console (Rails 3+) command to reload modified code?

I use the Rails console to try to figure out why the method in my model is not being called. The model is in. /app/models/product.rb. I am changing the method to calling self.trim_history on trim_history . I still hug my head when you call a method like self.do_stuff or just do_stuff .

Well, in the process of figuring this out, I ran into the reload! problem reload! . I realized that reload! will load all models and controllers again, but this does not seem to work. When I debug my code, the line that I changed as do_stuff still displays as self.do_stuff in the debugger.

I tried to solve this problem with reload! without correction. I reloaded the object from the database after rebooting, without a fix. I loaded the model directly with load './app/models/product.rb' and then loaded the object from db, without fixing it. I checked that the object is being modified using tail .

What is the best way to reload the model in rails console? Do I have to restart the console every time I change the model code? Is there a way to simply reset all existing models and their code without rebooting the console?

Edit : this has become weird. I now suspect that my code is reloading, but the debugger does not reflect the new code. When I look at the code, the old code is there, but the new code is executed. I checked this by adding raise - raise never displayed in the debugger, but it is raised. Wat?

Is there something I need to do with the debugger to see how it reloaded the code? This seems like a mistake ...

+6
source share
1 answer

reload! doesn't reinitialize existing objects, it just reloads the code. If you create a new instance of the class, it should reflect the changes.

+16
source

Source: https://habr.com/ru/post/914981/


All Articles