如何在rails控制台中重新加载模型?

时间:2022-12-27 20:46:55
> rvm list
rvm rubies

=* ruby-1.9.2-p290 [ i686 ]
   ruby-1.9.3-p194 [ i686 ]

> rails -v
Rails 3.1.1

Related, but didn't answer: Is there a Rails Console command (Rails 3+) to reload changed code?

相关,但没有回答:是否有Rails控制台命令(Rails 3+)重新加载更改的代码?

I'm using the Rails console to try and figure out why a method in my model isn't being called. The model is in ./app/models/product.rb. I'm changing a method from being called as self.trim_history to trim_history. I'm still wrapping my head around when to call a method as self.do_stuff or just do_stuff.

我正在使用Rails控制台尝试找出为什么我的模型中的方法没有被调用。该模型位于./app/models/product.rb中。我正在将一个方法从被调用为self.trim_history更改为trim_history。当我把一个方法称为self.do_stuff或者只是do_stuff时,我仍然在缠着头。

Well in the course of figuring this out I've run into a problem with reload!. My understanding was that reload! would load all the models and controllers again, but that doesn't seem to be working. When I debug my code, the line that I've changed to be do_stuff is still showing as self.do_stuff in the debugger.

好吧,在弄清楚这个过程中,我遇到了重装的问题!我的理解是重装!将再次加载所有模型和控制器,但这似乎不起作用。当我调试我的代码时,我更改为do_stuff的行仍然在调试器中显示为self.do_stuff。

I've attempted to solve this with reload!, no fix. I've reloaded the object from the database after reloading, no fix. I've loaded the model directly with load './app/models/product.rb', then loaded the object from the db, no fix. I've verified that the object is being changed with tail.

我试图通过重新加载解决这个问题!,没有修复。重新加载后我从数据库重新加载了对象,没有修复。我直接用加载'./app/models/product.rb'加载模型,然后从db加载对象,没有修复。我已经确认该对象正在改变尾部。

What's the best way to reload a model in the rails console? Do I have to re-start the console every time my model code changes? Is there some way to just dump all of the existing models and their code without reloading the console?

在rails控制台中重新加载模型的最佳方法是什么?每次模型代码更改时,是否必须重新启动控制台?有没有办法在不重新加载控制台的情况下转储所有现有模型及其代码?

Edit: this got stranger. I now suspect that my code is being reloaded but the debugger is not reflecting the new code. When I step through the code, the old code is there, but the new code is being executed. I tested this by adding a raise -- the raise never shows in the debugger but it is raised. Wat?

编辑:这个变得陌生。我现在怀疑我的代码正在重新加载,但调试器没有反映新代码。当我单步执行代码时,旧代码就在那里,但新代码正在执行。我通过添加一个加注来测试它 - 加注从未在调试器中显示但是它被提升了。笏?

Is there something I need to do with the debugger to get it to see the reloaded code? This looks like a bug...

我是否需要使用调试器来查看重新加载的代码?这看起来像个bug ...

1 个解决方案

#1


17  

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.

重装!不重新初始化现有对象,它只是重新加载代码。如果您创建类的新实例,它应该反映更改。

#1


17  

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.

重装!不重新初始化现有对象,它只是重新加载代码。如果您创建类的新实例,它应该反映更改。