0

I am using accessing my controllers methods from console with rails c command. The problem I am facing is that each time I have reflect any changes made in the code then I have to first exit and restart . Is these any way to fix this problem?

LearningBasics
  • 660
  • 1
  • 7
  • 24

2 Answers2

4

From your rails console, type reload!

2.1.2 :012 > reload!
Reloading...
 => true
2.1.2 :013 >

to reload all your Rails application code. No need to exit and start console again!

Raj
  • 22,346
  • 14
  • 99
  • 142
  • 3
    Yes, however there is this caveat: If you have any pre-existing objects, they will still be based on the previous code. So it's important to re-initialize any objects you are working with after a `reload!` command. – platforms Sep 12 '14 at 12:17
  • ^^ Exactly! + those objects will not have updated methods with the updated code and you may end up getting weird errors! – Surya Sep 12 '14 at 12:18
0

If you have associations you can do this:

class home
  belongs_to :renter
end

class renter
  has_one :home
end

Let's say you start with home attributes:

home = Home.where(renter_id: 1)
  => #< Home id: 1, alarm: "no">
renter = Renter.find(1)
renter.home.alarm
  => "no"

Then you modify home:

home.alarm = "yes"
home.save

When you do:

renter.home
  => #< Home id: 1, alarm: "no">   # it still returns no
renter.home(true)
  => #< Home id: 1, alarm: "yes">"  
        # you can use (true) to make sure your association 
        # change is reflected, it basically queries the server again
ecoding5
  • 404
  • 6
  • 19