Hi, so basically I’m starting with Rails (moving from Coldfusion/Java).
I’m following the Agile development with rails book, and I have this
method in my model, called ‘add_to_cart’, called from the controller.
Now I was modifying this method, reloaded the page, and noticed my
changed were not being taken into account. I tried clearing the browser
cache, nothing. I tried adding debug message in the method body,
nothing. I tried DELETING THE WHOLE METHOD - nothing - it was still
functionning as if the method was still there.
In CF it’s called ‘bytecode templates’ and they can be easily cleared
when CF chokes on them. I was wondering if there is such a think in
rails - or even better, a way to prevent rails from caching the model
at all when in ‘development’ mode.
TIA!
Well actually now I’m at the point where I have to restart the server
whenever I need to update the model… it’s getting annoying
By default, in development mode, your models will always reload on every
request. It sounds like your environment is not set up quite right. I’d
like
to hear more about your enviornment… OS, ruby version, whether you’re
using WEBrick or Apache, and what version of the Agile book you’re
using.
On 11/20/06, [email protected] [email protected]
On 11/20/06, [email protected] [email protected]
wrote:
Well actually now I’m at the point where I have to restart the server
whenever I need to update the model… it’s getting annoying
Have you accidentally switched your application into Production
environment? That’s when this caching occurs and changes only takes
effect when server is restarted. It is obviously not intended for
development.
Check in your %rails_app%/config/environment.rb and see if
“ENV[‘RAILS_ENV’]” is set to “production”, if so, comment that line
out and restart the server and you should be good. It can be set in
other places too though.
Hope that helps,
Mathias.
Well actually now I’m at the point where I have to restart the server
whenever I need to update the model… it’s getting annoying
Are you running in production? If so, switch to development and you’re
models should get reloaded on every page request.
If it’s a class (say in lib) put “include Reloadable” in it to have it
pick up changes all the time…
What you are seeing is not the norm.
-philip
Hi All, thanks for the replies!
OK I’ve checked, I’m running in DEV mode, not production or anything.
It’s not in a lib either.
I really don’t know what it could be… here’s the code for the class:
class Cart
attr_reader :items
attr_reader :total_price
def initialize
@items = []
@total_price = 0.0
end
def add_product(product)
item = @items.find {|i| i.product_id == product.id}
if item
item.quantity += 1
else
item = LineItem.for_product(product)
@items << item
end
@total_price += product.price
end
def empty!
@items = []
@total_price = 0.0
end
end
… as you can see it’s just ‘baby’ code - nothing special there.
I should have added, I can add or edit any method in there - only a
server restart makes it take the changes into account. I don’t have
that issue with my other class, just that one.
Problem solved!!! My problem was that Cart was the only object kept…
in the session hash. So there you have it, adding:
model :cart
… to the application controller solved the problem. Pfweeew!
Thank you all for your help!
[email protected] wrote:
Problem solved!!! My problem was that Cart was the only object kept…
in the session hash. So there you have it, adding:
model :cart
… to the application controller solved the problem. Pfweeew!
Thank you all for your help!
The reason this happens is that Rails wil lonly auto-reload models that
inherit form ActiveRecord. Your cart is an abstract table-less model
and doesn’t get flagged by Rails to be reloaded.
Adding include Reloadable marks the class as any ActiveRecord model
would be marked for reloading.
Also, in edge rails you wont need Reloadable anymore, it should just
happen on its own.
I’m having this problem (start webrick, change model, call new model
function > gives error “NoMethod…”). I’ve tried pretty much
everything. I’m running Rails 1.1.6 on OS X, Webrick. In my
“environments.rb” file I have ENV[‘RAILS_ENV’] == “development”, in my
“environments/development.rb” file I have config.cache_classes = false.
I’ve also tried lighttpd, and in lighttpd.conf i have
“bin-environment” => ( “RAILS_ENV” => “development” ). I can’t imagine
telling Rails anymore that I want to run it in development mode, but it
still won’t let me access new model methods without server restart.
The only way around this seems to be to put require “Reloadable” in
every model class. What am i doing wrong?
Sounds like you have your environment (RAILS_ENV) set to ‘production’.
If so, restarting the webservice (i.e. webrick) will clear the cache
out. Better yet, do development with your environment set to
‘development’. Then restarting the service won’t be necessary.
blakeage wrote:
In my
“environments.rb” file I have ENV[‘RAILS_ENV’] == “development”
If it wasn’t a typo, ‘==’ should be ‘=’
regards
Justin