简体   繁体   中英

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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