Scroll to top

Reloading Rails console without exiting


Ali - March 31, 2011 - 3 comments

In my journey to hopefully master Ruby on Rails, I’m amazed to see that many authors (in blogs or in books) most of whom are pretty expert in the field, choose to exit the Rails console completely (using CTRL+D) when they need to restart the environment (to clear up the objects in the memory, for example).

Instead there is a convenient method:

reload!

that you can type inside Rails console and it will reload the environment for you. No need to wait extra second to exit and reenter the console.

P.S. There might be a wisdom in not choosing realod! which I might not be aware of. In that case please enlighten me in the comments.

Related posts

3 comments

  1. Paolo

    It only reloads the code, no changes in the attributes of models
    http://gilesbowkett.blogspot.it/2007/08/evil-rails-console-gotcha-reload.html

    Anyway it’s fine if you’re debugging some code and want to see how changes play out.

  2. Ali

    Actually it does reflect the model changes. 🙂

    
    ruby-1.9.2-p290 :011 > Image.new
     => #<Image id: nil, title: nil, image_path: nil, created_at: nil, updated_at: nil, product_id: nil> 
    ruby-1.9.2-p290 :012 > reload!
    Reloading...
     => true 
    ruby-1.9.2-p290 :013 > Image.new
     => #<Image id: nil, title: nil, image_path: nil, created_at: nil, updated_at: nil> 
    

    I ran a migration after the first Image.new. That removed the product_id and a reload!, unless this is not what you mean.

  3. Sampson Crowley

    reload! only reloads application code, not configs or anything environment related

Post a Comment

Your email address will not be published. Required fields are marked *