什么是原因“重新加载”。并不总是似乎在Rails控制台中工作? [英] What are reasons "reload!" doesn't always seem to work in the Rails console?

查看:154
本文介绍了什么是原因“重新加载”。并不总是似乎在Rails控制台中工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是得到Rails控制台的悬挂,并发现它有用的快速测试方法在我的类。我知道我可以更改我的模型,然后

 >重装! 

来获取这些更新,但有时我会发现它似乎没有重新加载我的最新码。 Rails缓存代码在什么地方?



在一个非常简单的伪示例中,我可能在第100行有错误的代码:

 code> 100:u = User.alll 

,在Rails控制台中这个方法,我可能得到类似的错误:

  NoMethodError:undefined方法`alll'for User:Class ... on行100 

然后修改我的代码,修复错误

  100:u = User.all 

重新载入:

 >重装! 

然后,当调用此类中具有正确代码的方法时, / p>

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

当显然,错误是固定的,违规行不再是第100行。是否有办法强制/硬重置reload!命令?

解决方案

我的猜测是,你正在做像:


  1. 创建用户

  2. 的实例
  3. 调用 someMethod

  4. 您收到错误,然后解决问题

  5. reload! / code>

  6. 您在现有实例上调用 someMethod / li>

所以你要在一个没有重新加载的实例上调用这个方法。它的类已经重新加载,但实例已经在内存中 - 有错误和所有。



这将是我的猜测(不是100%确定)。

点是,如果你在 之后创建一个新的实例 重载!在这个新实例上,它应该停止抱怨。


I'm just getting the hang of Rails console, and finding it useful for quickly testing methods in my classes. 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?

In a really simple pseudo example, I may have bad code on line 100:

100: u = User.alll

and in the Rails console, when I run this method, I might get an error similar to:

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. 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
  2. Call someMethod on the instance
  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

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.

That would be my guess at least (not 100% sure).

Point is, if you create a new instance after the reload! and call your method on that new instance, it should stop complaining.

这篇关于什么是原因“重新加载”。并不总是似乎在Rails控制台中工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆