Rails caching the model - is there a way to disable it or clear the cache code?

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 :frowning:

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.

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. :slight_smile: 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 :frowning:

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.

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

Right here add...

    include Reloadable

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!

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.

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?

Brian Hogan wrote:

blakeage wrote:

In my "environments.rb" file I have ENV['RAILS_ENV'] == "development"

If it wasn't a typo, '==' should be '='

regards

   Justin