Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #6730 (closed defect: invalid)

Opened 3 years ago

Last modified 3 years ago

Losing value of class variables

Reported by: sfwalter Assigned to: nzkoz
Priority: normal Milestone: 1.2 regressions
Component: ActionPack Version:
Severity: normal Keywords:
Cc:

Description

I have a class that has a class variable which is a Hash. On the first web request the hash has the correct number of elements in it, on the second web request the hash 0 elements in it. It seems that the class is being reloaded.

Here is the class that I have inside my lib directory in a file called items.rb:

class Items

@@container = [] cattr_reader :container

def self.add_item(item)

@@container << item

end

end

I then have a simple plugin that does the following in the init.rb:

Items.add_item "item1" Items.add_item "item2"

I then have a controller like this:

class TestController < ApplicationController

def index

render :text => "#{Items.container.size}"

end

end

On the first web request "2" is being displayed which correct. On a refresh of the browser "0" is being displayed which is incorrect. I tried this in both development and production mode with the same results. If I revert back to Rails 1.1, the correct value of "2" is displayed on all web requests.

Weird.

I have all this packaged up in a test app I can send you if needed.

Attachments

regressions.zip (62.0 kB) - added by sfwalter on 11/29/06 19:37:21.

Change History

11/29/06 19:37:21 changed by sfwalter

  • attachment regressions.zip added.

(follow-up: ↓ 2 ) 11/29/06 23:22:09 changed by nzkoz

  • owner changed from David to nzkoz.
  • status changed from new to assigned.

I believe this is caused by reloading, can you verify that:

1) It doesn't occur in production mode; and 2) It doesn't occur when you have require 'items' in environment.rb

(in reply to: ↑ 1 ) 11/30/06 00:31:19 changed by sfwalter

Replying to nzkoz:

I believe this is caused by reloading, can you verify that: 1) It doesn't occur in production mode; and 2) It doesn't occur when you have require 'items' in environment.rb

1) The problem also occurs in production mode 2) If I add require 'items' to the bottom of environment.rb, it doesn't work at all. Not even on the first request.

12/05/06 01:30:28 changed by sfwalter

Ok so I found the blog post about the new reloading scheme. I added the following outside the initializer block in the environment.rb file:

Dependencies.log_activity = true require "#{RAILS_ROOT}/lib/items.rb"

Then when I make a web request I see this:

Dependencies: removing constant Items

I thought doing a require would prevent the file from getting autoloaded on each request.

12/05/06 01:47:11 changed by sfwalter

Ok I think I found the solution. First the problem was that since lib is one the auto load directories and I was doing a require at the end of the environment.rb file, it was being marked as auto loaded. If I moved the require to the top of environment.rb everything is happy.

12/07/06 08:43:58 changed by evan

  • status changed from assigned to closed.
  • resolution set to invalid.

03/04/07 13:47:53 changed by bitsweat

  • keywords deleted.
  • milestone changed from 1.2 to 1.2 regressions.