Wednesday, December 06, 2006

Rails - Automatic Reloading and Models

I'm fairly new to RoR. I have to admit that I'm pretty excited about exiting the Code-Compile-Deploy-Run cycle because of config.cache_classes = false in \environments\development.rb. However, I've discovered that doesn't apply to Models. Models are loaded at server startup. Therefore, we're back to the stop..start to pickup Models changes. I haven't tried either of these yet!

Two options:

2 comments:

Roger Pack said...

I usually do something like
watcher_thread = Thread.new{
print 'starting watcher'
latest_inserted = Time.now
dirs = ['app/controllers', 'app/schools', '/app/models']
loop do
has_new = false
for dir in dirs
for file in Dir.glob dir + '/*'
time = File.ctime file
if time > latest_inserted
has_new = true
print 'got new' , file
break
end
end
break if has_new
end
if has_new
system_command = "kill -9 #{Process.pid}"
p 'running', system_command
system(system_command) # we are done
latest_inserted = Time.now
Process.kill!
Process.kill
Process::exit
end
sleep 0.2
end
} if ENV['RAILS_ENV'] == 'development' # ruby does it itself otherwise, I think. There may be a rails way to do this.

and then run the server inside a script that will start it automatically
like
run_forever.rb:
loop do
system ARGV.join(' ')
end
then
ruby run_forever.rb script/server -e development

then it works!

Jason said...

I'm a Windows person, so the kill -9 won't work for me. Either way, this is a cool script. Thanks, Jason