简体   繁体   English

什么是“重新加载!”的原因似乎并不总是在Rails控制台中工作?

[英]What are reasons “reload!” doesn't always seem to work in the Rails console?

I'm just getting the hang of Rails console, and finding it useful for quickly testing methods in my classes. 我只是掌握了Rails控制台,并发现它可以快速测试我的类中的方法。 I know that I can make changes to my Models, then 我知道我可以对我的模型进行更改

> reload!

to grab those updates, but sometimes I'll find that it doesn't seem to reload my latest code. 抓住这些更新,但有时我会发现它似乎没有重新加载我的最新代码。 Does Rails cache code somewhere? Rails是否在某处缓存代码?

In a really simple pseudo example, I may have bad code on line 100: 在一个非常简单的伪示例中,我可能在第100行上有错误的代码:

100: u = User.alll

and in the Rails console, when I run this method, I might get an error similar to: 在Rails控制台中,当我运行此方法时,我可能会收到类似于以下内容的错误:

NoMethodError: undefined method `alll' for User:Class ... on line 100

then modify my code, fixing the error 然后修改我的代码,修复错误

100: u = User.all

then reload: 然后重新加载:

> reload!

and then, when calling the method in this class that has the correct code, it still will say 然后,当在这个类中调用具有正确代码的方法时,它仍会说

NoMethodError: undefined method `alll' for User:Class ... on line 100

When clearly, the error is fixed, and the offending line isn't even on line 100 anymore. 如果清楚,错误是固定的,并且违规行甚至不再在第100行。 Is there a way to force/hard-reset the "reload!" 有没有办法强制/硬重置“重新加载!” command? 命令?

My guess would be that you're doing something like: 我的猜测是你正在做的事情:

  1. Create an instance of User 创建User实例
  2. Call someMethod on the instance 在实例上调用someMethod
  3. You get an error, and you go and fix it 你得到一个错误,然后去解决它
  4. reload!
  5. You call someMethod on the existing instance and get the error again 在现有实例上调用someMethod并再次获取错误

So you're calling the method on an instance that hasn't itself been reloaded. 所以你在一个本身没有重新加载的实例上调用该方法。 Its class has been reloaded, but the instance is already in memory - with bugs and all. 它的类已被重新加载,但实例已经在内存中 - 有bug和所有。

That would be my guess at least (not 100% sure). 这至少是我的猜测(不是100%肯定)。

Point is, if you create a new instance after the reload! 点是,如果你在reload! 创建一个新实例reload! and call your method on that new instance, it should stop complaining. 并在新实例上调用您的方法,它应该停止抱怨。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM