Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 248
jbnunn Avatar asked Jun 27 '12 22:06

jbnunn


People also ask

What does reload do in rails console?

Reload: This command will allow you to make changes to your code, and continue to use the same console session without having to restart. Simply type in the “reload!” command after making changes and the console will reload the session.

What does Rails console do?

The console command lets you interact with your Rails application from the command line. On the underside, bin/rails console uses IRB, so if you've ever used it, you'll be right at home. This is useful for testing out quick ideas with code and changing data server-side without touching the website.


1 Answers

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.

like image 129
Flambino Avatar answered Oct 06 '22 23:10

Flambino