<?xml version="1.0" encoding="UTF-8"?>
<ticket>
  <assigned-user-id type="integer" nil="true"></assigned-user-id>
  <attachments-count type="integer">4</attachments-count>
  <closed type="boolean">false</closed>
  <created-at type="datetime">2008-11-06T21:36:17+00:00</created-at>
  <creator-id type="integer">28420</creator-id>
  <milestone-due-on type="datetime" nil="true"></milestone-due-on>
  <milestone-id type="integer">9903</milestone-id>
  <number type="integer">1339</number>
  <permalink>arbase-should-not-be-nuking-its-children-just-because-it-lost-interest</permalink>
  <priority type="integer">647</priority>
  <project-id type="integer">8994</project-id>
  <raw-data type="binary" nil="true" encoding="base64"></raw-data>
  <state>open</state>
  <tag>activerecord bug dependencies reload</tag>
  <title>AR::Base should not be nuking its children, just because it lost interest</title>
  <updated-at type="datetime">2009-10-22T05:13:29+01:00</updated-at>
  <user-id type="integer">73743</user-id>
  <user-name>Sai Emrys</user-name>
  <creator-name>Tom Lea</creator-name>
  <url>http://rails.lighthouseapp.com/projects/8994/tickets/1339</url>
  <milestone-title>2.x</milestone-title>
  <original-body>I diagnosed the following issue as part of [ref1], but it has since come  
up again as part of [ref2]. 

This new ticket is to discuss it in isolation.

## The issue:

Whenever an AR::Base instance is persisted between requests with  
cache_classes disabled, Dispatcher#reset_application! will go through  
all its subclasses and remove all its instance methods and instance  
variables (those on the instance of Class not on instances of  
AR::Base, it's all very confusing!).

This was intended to fix a memory leak [ref3], and had a test associated  
with it. The test is now long gone, and my efforts at re-adding it  
failed because I never got it to pass, with or without the class  
stripping.

## The effect:

The example app by David Rice [ref4] will explode with error [ref5] after 2  
page loads.

This app saves an AR model to a constant at startup... then expects it  
to work after a reload!.

After a reload the model gets very confused, as its class has all its  
class_inheritable_attributes stripped.

It expects an array to be dangling off the end of  
`#skip_time_zone_conversion_for_attributes`, but instead it finds nil.

This sucks.

## What should happen (IMHO):

Removing the references to classes in @@subclasses should be enough to  
allow garbage collection to do the good stuff when the relevant  
constants (that refer to the classes) are removed.

This way, any objects that are still referencing those classes (you  
know, like when they are an instance of them), can still operate  
without unnecessary loss of organs.

## The patch:

The attached patch, rebased especially for the occasion, stops AR from  
behaving in this odd way, and seems to pass the existing test pack.

## The questions:

Does anyone have a good reason why these lines should stay?

Can anyone create a failing test case? (preferably one that passes  
before the patch is applied ;)

Does anyone remember why the code is there?


Thanks in advance for any thoughts / input / other.

   - Tom

p.s.

I had to revert my local rails back to 55707da1a1481, as 8adb79b9b598  
onwards clashed with my MySQL.

Tests passed at that point though.

### References
* [ref1] http://rails.lighthouseapp.com/projects/8994/tickets/785
* [ref2] http://rails.lighthouseapp.com/projects/8994/tickets/1290
* [ref3] http://github.com/rails/rails/commit/e7f61eab56d8a628cc605e5fa1e9055c4df852c7
* [ref4] http://rails.lighthouseapp.com/attachments/59425/lighthouse-ticket-1290.zip
* [ref5] http://pastie.org/308884</original-body>
  <latest-body>I diagnosed the following issue as part of [ref1], but it has since come  
up again as part of [ref2]. 

This new ticket is to discuss it in isolation.

## The issue:

Whenever an AR::Base instance is persisted between requests with  
cache_classes disabled, Dispatcher#reset_application! will go through  
all its subclasses and remove all its instance methods and instance  
variables (those on the instance of Class not on instances of  
AR::Base, it's all very confusing!).

This was intended to fix a memory leak [ref3], and had a test associated  
with it. The test is now long gone, and my efforts at re-adding it  
failed because I never got it to pass, with or without the class  
stripping.

## The effect:

The example app by David Rice [ref4] will explode with error [ref5] after 2  
page loads.

This app saves an AR model to a constant at startup... then expects it  
to work after a reload!.

After a reload the model gets very confused, as its class has all its  
class_inheritable_attributes stripped.

It expects an array to be dangling off the end of  
`#skip_time_zone_conversion_for_attributes`, but instead it finds nil.

This sucks.

## What should happen (IMHO):

Removing the references to classes in @@subclasses should be enough to  
allow garbage collection to do the good stuff when the relevant  
constants (that refer to the classes) are removed.

This way, any objects that are still referencing those classes (you  
know, like when they are an instance of them), can still operate  
without unnecessary loss of organs.

## The patch:

The attached patch, rebased especially for the occasion, stops AR from  
behaving in this odd way, and seems to pass the existing test pack.

## The questions:

Does anyone have a good reason why these lines should stay?

Can anyone create a failing test case? (preferably one that passes  
before the patch is applied ;)

Does anyone remember why the code is there?


Thanks in advance for any thoughts / input / other.

   - Tom

p.s.

I had to revert my local rails back to 55707da1a1481, as 8adb79b9b598  
onwards clashed with my MySQL.

Tests passed at that point though.

### References
* [ref1] http://rails.lighthouseapp.com/projects/8994/tickets/785
* [ref2] http://rails.lighthouseapp.com/projects/8994/tickets/1290
* [ref3] http://github.com/rails/rails/commit/e7f61eab56d8a628cc605e5fa1e9055c4df852c7
* [ref4] http://rails.lighthouseapp.com/attachments/59425/lighthouse-ticket-1290.zip
* [ref5] http://pastie.org/308884</latest-body>
  <original-body-html>&lt;div&gt;&lt;p&gt;I diagnosed the following issue as part of [ref1], but it has
since come up again as part of [ref2].&lt;/p&gt;
&lt;p&gt;This new ticket is to discuss it in isolation.&lt;/p&gt;
&lt;h2&gt;The issue:&lt;/h2&gt;
&lt;p&gt;Whenever an AR::Base instance is persisted between requests with
cache_classes disabled, Dispatcher#reset_application! will go
through all its subclasses and remove all its instance methods and
instance variables (those on the instance of Class not on instances
of AR::Base, it's all very confusing!).&lt;/p&gt;
&lt;p&gt;This was intended to fix a memory leak [ref3], and had a test
associated with it. The test is now long gone, and my efforts at
re-adding it failed because I never got it to pass, with or without
the class stripping.&lt;/p&gt;
&lt;h2&gt;The effect:&lt;/h2&gt;
&lt;p&gt;The example app by David Rice [ref4] will explode with error
[ref5] after 2 page loads.&lt;/p&gt;
&lt;p&gt;This app saves an AR model to a constant at startup... then
expects it to work after a reload!.&lt;/p&gt;
&lt;p&gt;After a reload the model gets very confused, as its class has
all its class_inheritable_attributes stripped.&lt;/p&gt;
&lt;p&gt;It expects an array to be dangling off the end of
&lt;code&gt;#skip_time_zone_conversion_for_attributes&lt;/code&gt;, but instead
it finds nil.&lt;/p&gt;
&lt;p&gt;This sucks.&lt;/p&gt;
&lt;h2&gt;What should happen (IMHO):&lt;/h2&gt;
&lt;p&gt;Removing the references to classes in @@subclasses should be
enough to allow garbage collection to do the good stuff when the
relevant constants (that refer to the classes) are removed.&lt;/p&gt;
&lt;p&gt;This way, any objects that are still referencing those classes
(you know, like when they are an instance of them), can still
operate without unnecessary loss of organs.&lt;/p&gt;
&lt;h2&gt;The patch:&lt;/h2&gt;
&lt;p&gt;The attached patch, rebased especially for the occasion, stops
AR from behaving in this odd way, and seems to pass the existing
test pack.&lt;/p&gt;
&lt;h2&gt;The questions:&lt;/h2&gt;
&lt;p&gt;Does anyone have a good reason why these lines should stay?&lt;/p&gt;
&lt;p&gt;Can anyone create a failing test case? (preferably one that
passes before the patch is applied ;)&lt;/p&gt;
&lt;p&gt;Does anyone remember why the code is there?&lt;/p&gt;
&lt;p&gt;Thanks in advance for any thoughts / input / other.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Tom&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;p.s.&lt;/p&gt;
&lt;p&gt;I had to revert my local rails back to 55707da1a1481, as
8adb79b9b598 onwards clashed with my MySQL.&lt;/p&gt;
&lt;p&gt;Tests passed at that point though.&lt;/p&gt;
&lt;h3&gt;References&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;[ref1] &lt;a href=&quot;http://rails.lighthouseapp.com/projects/8994/tickets/785&quot;&gt;http://rails.lighthouseapp.com/p...&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;[ref2] &lt;a href=&quot;http://rails.lighthouseapp.com/projects/8994/tickets/1290&quot;&gt;http://rails.lighthouseapp.com/p...&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;[ref3] &lt;a href=&quot;http://github.com/rails/rails/commit/e7f61eab56d8a628cc605e5fa1e9055c4df852c7&quot;&gt;
http://github.com/rails/rails/co...&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;[ref4] &lt;a href=&quot;http://rails.lighthouseapp.com/attachments/59425/lighthouse-ticket-1290.zip&quot;&gt;
http://rails.lighthouseapp.com/a...&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;[ref5] &lt;a href=&quot;http://pastie.org/308884&quot;&gt;http://pastie.org/308884&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;/div&gt;</original-body-html>
  <versions type="array">
    <version type="Ticket::Version">
      <assigned-user-id type="integer" nil="true"></assigned-user-id>
      <attachments-count type="integer">0</attachments-count>
      <body>I diagnosed the following issue as part of [ref1], but it has since come  
up again as part of [ref2]. 

This new ticket is to discuss it in isolation.

## The issue:

Whenever an AR::Base instance is persisted between requests with  
cache_classes disabled, Dispatcher#reset_application! will go through  
all its subclasses and remove all its instance methods and instance  
variables (those on the instance of Class not on instances of  
AR::Base, it's all very confusing!).

This was intended to fix a memory leak [ref3], and had a test associated  
with it. The test is now long gone, and my efforts at re-adding it  
failed because I never got it to pass, with or without the class  
stripping.

## The effect:

The example app by David Rice [ref4] will explode with error [ref5] after 2  
page loads.

This app saves an AR model to a constant at startup... then expects it  
to work after a reload!.

After a reload the model gets very confused, as its class has all its  
class_inheritable_attributes stripped.

It expects an array to be dangling off the end of  
`#skip_time_zone_conversion_for_attributes`, but instead it finds nil.

This sucks.

## What should happen (IMHO):

Removing the references to classes in @@subclasses should be enough to  
allow garbage collection to do the good stuff when the relevant  
constants (that refer to the classes) are removed.

This way, any objects that are still referencing those classes (you  
know, like when they are an instance of them), can still operate  
without unnecessary loss of organs.

## The patch:

The attached patch, rebased especially for the occasion, stops AR from  
behaving in this odd way, and seems to pass the existing test pack.

## The questions:

Does anyone have a good reason why these lines should stay?

Can anyone create a failing test case? (preferably one that passes  
before the patch is applied ;)

Does anyone remember why the code is there?

Thanks in advance for any thoughts / input / other.

### References
* [ref1] http://rails.lighthouseapp.com/projects/8994/tickets/785
* [ref2] http://rails.lighthouseapp.com/projects/8994/tickets/1290
* [ref3] http://github.com/rails/rails/commit/e7f61eab56d8a628cc605e5fa1e9055c4df852c7
* [ref4] http://rails.lighthouseapp.com/attachments/59425/lighthouse-ticket-1290.zip
* [ref5] http://pastie.org/308884</body>
      <body-html>&lt;div&gt;&lt;p&gt;I diagnosed the following issue as part of [ref1], but it has
since come up again as part of [ref2].&lt;/p&gt;
&lt;p&gt;This new ticket is to discuss it in isolation.&lt;/p&gt;
&lt;h2&gt;The issue:&lt;/h2&gt;
&lt;p&gt;Whenever an AR::Base instance is persisted between requests with
cache_classes disabled, Dispatcher#reset_application! will go
through all its subclasses and remove all its instance methods and
instance variables (those on the instance of Class not on instances
of AR::Base, it's all very confusing!).&lt;/p&gt;
&lt;p&gt;This was intended to fix a memory leak [ref3], and had a test
associated with it. The test is now long gone, and my efforts at
re-adding it failed because I never got it to pass, with or without
the class stripping.&lt;/p&gt;
&lt;h2&gt;The effect:&lt;/h2&gt;
&lt;p&gt;The example app by David Rice [ref4] will explode with error
[ref5] after 2 page loads.&lt;/p&gt;
&lt;p&gt;This app saves an AR model to a constant at startup... then
expects it to work after a reload!.&lt;/p&gt;
&lt;p&gt;After a reload the model gets very confused, as its class has
all its class_inheritable_attributes stripped.&lt;/p&gt;
&lt;p&gt;It expects an array to be dangling off the end of
&lt;code&gt;#skip_time_zone_conversion_for_attributes&lt;/code&gt;, but instead
it finds nil.&lt;/p&gt;
&lt;p&gt;This sucks.&lt;/p&gt;
&lt;h2&gt;What should happen (IMHO):&lt;/h2&gt;
&lt;p&gt;Removing the references to classes in @@subclasses should be
enough to allow garbage collection to do the good stuff when the
relevant constants (that refer to the classes) are removed.&lt;/p&gt;
&lt;p&gt;This way, any objects that are still referencing those classes
(you know, like when they are an instance of them), can still
operate without unnecessary loss of organs.&lt;/p&gt;
&lt;h2&gt;The patch:&lt;/h2&gt;
&lt;p&gt;The attached patch, rebased especially for the occasion, stops
AR from behaving in this odd way, and seems to pass the existing
test pack.&lt;/p&gt;
&lt;h2&gt;The questions:&lt;/h2&gt;
&lt;p&gt;Does anyone have a good reason why these lines should stay?&lt;/p&gt;
&lt;p&gt;Can anyone create a failing test case? (preferably one that
passes before the patch is applied ;)&lt;/p&gt;
&lt;p&gt;Does anyone remember why the code is there?&lt;/p&gt;
&lt;p&gt;Thanks in advance for any thoughts / input / other.&lt;/p&gt;
&lt;h3&gt;References&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;[ref1] &lt;a href=&quot;http://rails.lighthouseapp.com/projects/8994/tickets/785&quot;&gt;http://rails.lighthouseapp.com/p...&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;[ref2] &lt;a href=&quot;http://rails.lighthouseapp.com/projects/8994/tickets/1290&quot;&gt;http://rails.lighthouseapp.com/p...&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;[ref3] &lt;a href=&quot;http://github.com/rails/rails/commit/e7f61eab56d8a628cc605e5fa1e9055c4df852c7&quot;&gt;
http://github.com/rails/rails/co...&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;[ref4] &lt;a href=&quot;http://rails.lighthouseapp.com/attachments/59425/lighthouse-ticket-1290.zip&quot;&gt;
http://rails.lighthouseapp.com/a...&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;[ref5] &lt;a href=&quot;http://pastie.org/308884&quot;&gt;http://pastie.org/308884&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;/div&gt;</body-html>
      <closed type="boolean">false</closed>
      <created-at type="datetime">2008-11-06T21:36:17+00:00</created-at>
      <creator-id type="integer">28420</creator-id>
      <diffable-attributes type="yaml">--- {}

</diffable-attributes>
      <milestone-id type="integer">9903</milestone-id>
      <number type="integer">1339</number>
      <permalink>arbase-should-not-be-nuking-its-children-just-because-it-lost-interest</permalink>
      <priority type="integer">0</priority>
      <project-id type="integer">8994</project-id>
      <state>new</state>
      <tag>activerecord bug reload</tag>
      <title>AR::Base should not be nuking it's children, just because it lost interest</title>
      <updated-at type="datetime">2008-11-06T21:36:49+00:00</updated-at>
      <user-id type="integer">28420</user-id>
      <user-name>Tom Lea</user-name>
      <creator-name>Tom Lea</creator-name>
      <url>http://rails.lighthouseapp.com/projects/8994/tickets/1339</url>
      <milestone-title>2.x</milestone-title>
    </version>
    <version type="Ticket::Version">
      <assigned-user-id type="integer">141</assigned-user-id>
      <attachments-count type="integer">1</attachments-count>
      <body>Git blame + git show points those lines as fixing a memory leak in dev mode:

http://github.com/rails/rails/commit/e7f61eab56d8a628cc605e5fa1e9055c4df852c7

I believe we removed that test case, but there's the reason.

Can you confirm that there's no leak in dev mode after applying this patch?</body>
      <body-html>&lt;div&gt;&lt;p&gt;Git blame + git show points those lines as fixing a memory leak
in dev mode:&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;http://github.com/rails/rails/commit/e7f61eab56d8a628cc605e5fa1e9055c4df852c7&quot;&gt;
http://github.com/rails/rails/co...&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;I believe we removed that test case, but there's the reason.&lt;/p&gt;
&lt;p&gt;Can you confirm that there's no leak in dev mode after applying
this patch?&lt;/p&gt;&lt;/div&gt;</body-html>
      <closed type="boolean">false</closed>
      <created-at type="datetime">2008-11-07T07:39:21+00:00</created-at>
      <creator-id type="integer">28420</creator-id>
      <diffable-attributes type="yaml">--- 
:assigned_user: 
</diffable-attributes>
      <milestone-id type="integer">9903</milestone-id>
      <number type="integer">1339</number>
      <permalink>arbase-should-not-be-nuking-its-children-just-because-it-lost-interest</permalink>
      <priority type="integer">0</priority>
      <project-id type="integer">8994</project-id>
      <state>new</state>
      <tag>activerecord bug reload</tag>
      <title>AR::Base should not be nuking it's children, just because it lost interest</title>
      <updated-at type="datetime">2008-11-07T07:39:24+00:00</updated-at>
      <user-id type="integer">141</user-id>
      <user-name>Michael Koziarski</user-name>
      <creator-name>Tom Lea</creator-name>
      <url>http://rails.lighthouseapp.com/projects/8994/tickets/1339</url>
      <milestone-title>2.x</milestone-title>
      <assigned-user-name>Michael Koziarski</assigned-user-name>
    </version>
    <version type="Ticket::Version">
      <assigned-user-id type="integer">141</assigned-user-id>
      <attachments-count type="integer">1</attachments-count>
      <body>for additional information, the leak previously was caused by the use of define_method() and reloading.  The procs used by define_method were held on to indefinitely, despite them not being in use any more.

If this has been fixed in a recent ruby version, then perhaps it's not a big deal.  But it was a complete disaster previously.</body>
      <body-html>&lt;div&gt;&lt;p&gt;for additional information, the leak previously was caused by
the use of define_method() and reloading. The procs used by
define_method were held on to indefinitely, despite them not being
in use any more.&lt;/p&gt;
&lt;p&gt;If this has been fixed in a recent ruby version, then perhaps
it's not a big deal. But it was a complete disaster previously.&lt;/p&gt;&lt;/div&gt;</body-html>
      <closed type="boolean">false</closed>
      <created-at type="datetime">2008-11-07T07:50:48+00:00</created-at>
      <creator-id type="integer">28420</creator-id>
      <diffable-attributes type="yaml">--- {}

</diffable-attributes>
      <milestone-id type="integer">9903</milestone-id>
      <number type="integer">1339</number>
      <permalink>arbase-should-not-be-nuking-its-children-just-because-it-lost-interest</permalink>
      <priority type="integer">0</priority>
      <project-id type="integer">8994</project-id>
      <state>new</state>
      <tag>activerecord bug reload</tag>
      <title>AR::Base should not be nuking it's children, just because it lost interest</title>
      <updated-at type="datetime">2008-11-07T07:50:53+00:00</updated-at>
      <user-id type="integer">141</user-id>
      <user-name>Michael Koziarski</user-name>
      <creator-name>Tom Lea</creator-name>
      <url>http://rails.lighthouseapp.com/projects/8994/tickets/1339</url>
      <milestone-title>2.x</milestone-title>
      <assigned-user-name>Michael Koziarski</assigned-user-name>
    </version>
    <version type="Ticket::Version">
      <assigned-user-id type="integer">141</assigned-user-id>
      <attachments-count type="integer">1</attachments-count>
      <body>Can we make this a 2.2 priority?</body>
      <body-html>&lt;div&gt;&lt;p&gt;Can we make this a 2.2 priority?&lt;/p&gt;&lt;/div&gt;</body-html>
      <closed type="boolean">false</closed>
      <created-at type="datetime">2008-11-10T18:42:14+00:00</created-at>
      <creator-id type="integer">28420</creator-id>
      <diffable-attributes type="yaml">--- 
:state: new
:milestone: 9903
</diffable-attributes>
      <milestone-id type="integer" nil="true"></milestone-id>
      <number type="integer">1339</number>
      <permalink>arbase-should-not-be-nuking-its-children-just-because-it-lost-interest</permalink>
      <priority type="integer">0</priority>
      <project-id type="integer">8994</project-id>
      <state>open</state>
      <tag>activerecord bug reload</tag>
      <title>AR::Base should not be nuking it's children, just because it lost interest</title>
      <updated-at type="datetime">2008-11-10T18:42:17+00:00</updated-at>
      <user-id type="integer">424</user-id>
      <user-name>Joshua Peek</user-name>
      <creator-name>Tom Lea</creator-name>
      <url>http://rails.lighthouseapp.com/projects/8994/tickets/1339</url>
      <milestone-title nil="true"></milestone-title>
      <assigned-user-name>Michael Koziarski</assigned-user-name>
    </version>
    <version type="Ticket::Version">
      <assigned-user-id type="integer">141</assigned-user-id>
      <attachments-count type="integer">1</attachments-count>
      <body>Not with the current implementation.

At present it does not affect anyone running with cache_classes = true, and it leaks (or leaked, no one has done more testing) when the patch is applied.

This is an old issue, and shouldn't block a release, though it'd be nice to have it fixed obviously :)</body>
      <body-html>&lt;div&gt;&lt;p&gt;Not with the current implementation.&lt;/p&gt;
&lt;p&gt;At present it does not affect anyone running with cache_classes
= true, and it leaks (or leaked, no one has done more testing) when
the patch is applied.&lt;/p&gt;
&lt;p&gt;This is an old issue, and shouldn't block a release, though it'd
be nice to have it fixed obviously :)&lt;/p&gt;&lt;/div&gt;</body-html>
      <closed type="boolean">false</closed>
      <created-at type="datetime">2008-11-10T19:56:02+00:00</created-at>
      <creator-id type="integer">28420</creator-id>
      <diffable-attributes type="yaml">--- 
:milestone: 12671
</diffable-attributes>
      <milestone-id type="integer">9903</milestone-id>
      <number type="integer">1339</number>
      <permalink>arbase-should-not-be-nuking-its-children-just-because-it-lost-interest</permalink>
      <priority type="integer">0</priority>
      <project-id type="integer">8994</project-id>
      <state>open</state>
      <tag>activerecord bug reload</tag>
      <title>AR::Base should not be nuking it's children, just because it lost interest</title>
      <updated-at type="datetime">2008-11-10T19:56:08+00:00</updated-at>
      <user-id type="integer">141</user-id>
      <user-name>Michael Koziarski</user-name>
      <creator-name>Tom Lea</creator-name>
      <url>http://rails.lighthouseapp.com/projects/8994/tickets/1339</url>
      <milestone-title>2.x</milestone-title>
      <assigned-user-name>Michael Koziarski</assigned-user-name>
    </version>
    <version type="Ticket::Version">
      <assigned-user-id type="integer">141</assigned-user-id>
      <attachments-count type="integer">1</attachments-count>
      <body>I disagree with Michael Koziarski.  This has caused a lot of headaches for a lot of people, and it would be nice if it could be fixed for the next release.

See http://rails.lighthouseapp.com/projects/8994/tickets/785-caching-models-fails-in-development for a long list of complaints. :)</body>
      <body-html>&lt;div&gt;&lt;p&gt;I disagree with Michael Koziarski. This has caused a lot of
headaches for a lot of people, and it would be nice if it could be
fixed for the next release.&lt;/p&gt;
&lt;p&gt;See &lt;a href=&quot;http://rails.lighthouseapp.com/projects/8994/tickets/785-caching-models-fails-in-development&quot;&gt;
http://rails.lighthouseapp.com/p...&lt;/a&gt; for a long list of
complaints. :)&lt;/p&gt;&lt;/div&gt;</body-html>
      <closed type="boolean">false</closed>
      <created-at type="datetime">2008-11-10T20:22:00+00:00</created-at>
      <creator-id type="integer">28420</creator-id>
      <diffable-attributes type="yaml">--- {}

</diffable-attributes>
      <milestone-id type="integer">9903</milestone-id>
      <number type="integer">1339</number>
      <permalink>arbase-should-not-be-nuking-its-children-just-because-it-lost-interest</permalink>
      <priority type="integer">0</priority>
      <project-id type="integer">8994</project-id>
      <state>open</state>
      <tag>activerecord bug reload</tag>
      <title>AR::Base should not be nuking it's children, just because it lost interest</title>
      <updated-at type="datetime">2008-11-10T20:22:02+00:00</updated-at>
      <user-id type="integer">17345</user-id>
      <user-name>Steven Soroka</user-name>
      <creator-name>Tom Lea</creator-name>
      <url>http://rails.lighthouseapp.com/projects/8994/tickets/1339</url>
      <milestone-title>2.x</milestone-title>
      <assigned-user-name>Michael Koziarski</assigned-user-name>
    </version>
    <version type="Ticket::Version">
      <assigned-user-id type="integer">141</assigned-user-id>
      <attachments-count type="integer">1</attachments-count>
      <body>I don't disagree that this is an annoying issue, but the fix in the other ticket to add a marshalling memory store does just as good a job as this patch, without leaking memory.

If you want to help out here, you should test this current patch against the current major versions of ruby (1.8.6, 1.8.7 and whatever debian stable is on) and report back if the code leaks memory in development mode.</body>
      <body-html>&lt;div&gt;&lt;p&gt;I don't disagree that this is an annoying issue, but the fix in
the other ticket to add a marshalling memory store does just as
good a job as this patch, without leaking memory.&lt;/p&gt;
&lt;p&gt;If you want to help out here, you should test this current patch
against the current major versions of ruby (1.8.6, 1.8.7 and
whatever debian stable is on) and report back if the code leaks
memory in development mode.&lt;/p&gt;&lt;/div&gt;</body-html>
      <closed type="boolean">false</closed>
      <created-at type="datetime">2008-11-10T20:26:37+00:00</created-at>
      <creator-id type="integer">28420</creator-id>
      <diffable-attributes type="yaml">--- {}

</diffable-attributes>
      <milestone-id type="integer">9903</milestone-id>
      <number type="integer">1339</number>
      <permalink>arbase-should-not-be-nuking-its-children-just-because-it-lost-interest</permalink>
      <priority type="integer">0</priority>
      <project-id type="integer">8994</project-id>
      <state>open</state>
      <tag>activerecord bug reload</tag>
      <title>AR::Base should not be nuking it's children, just because it lost interest</title>
      <updated-at type="datetime">2008-11-10T20:26:42+00:00</updated-at>
      <user-id type="integer">141</user-id>
      <user-name>Michael Koziarski</user-name>
      <creator-name>Tom Lea</creator-name>
      <url>http://rails.lighthouseapp.com/projects/8994/tickets/1339</url>
      <milestone-title>2.x</milestone-title>
      <assigned-user-name>Michael Koziarski</assigned-user-name>
    </version>
    <version type="Ticket::Version">
      <assigned-user-id type="integer">141</assigned-user-id>
      <attachments-count type="integer">1</attachments-count>
      <body>So I did a little memory profiling, it was a little hacky, so my methods may be bad. If this is so, tell me how to do it better.

I restarted the server each time, and switched branches when appropriate.

## ruby -v
ruby 1.8.6 (2008-03-03 patchlevel 114) [i686-darwin9.4.0]

(I ran the test against 1.8.7, freshly downloaded today, and the result were almost identical, 1.9{,.1} fail to start due to #827 on 1.9 and some other issue that ran over the end of my lunch hour on 1.9.1)

## Test Script

@@@ ruby
#!/usr/bin/env ruby

pid = `ps | grep script/serve[r] | awk '{ print $1 }'`.chomp
puts &quot;Server PID: #{pid}&quot;

url = ARGV.last
puts &quot;URL: #{url}&quot;

puts &quot;Hit enter to go.&quot;
$stdin.gets

def get_memory_usage(pid)
  `ps -o rss,vsz -p#{pid} | tail -n1`.chomp
end

puts &quot;Initial Memory Usage: #{get_memory_usage(pid)}&quot;

1000.times do
  system %Q{wget -O/dev/null -o/dev/null #{url}}
end

puts &quot;Final Memory Usage: #{get_memory_usage(pid)}&quot;

@@@

## Without Patch

@@@
                        RSS      VSZ
Initial Memory Usage:  31568   107692
Final Memory Usage:    43400   128408
Delta:                 11832   20716

Initial Memory Usage:  31568   107692
Final Memory Usage:    43460   128628
Delta:                 11892   20936

Initial Memory Usage:  31568   107692
Final Memory Usage:    43320   128248
Delta:                 11752   20556

Mean Delta:            11825   20736

## With Patch

Initial Memory Usage:  31568   107692
Final Memory Usage:    43584   128308
Delta:                 12016   20616

Initial Memory Usage:  31564   107692
Final Memory Usage:    43656   128468
Delta:                 12092   20776

Initial Memory Usage:  31612   107736
Final Memory Usage:    43644   128356
Delta:                 12032   20620

Mean Delta:            12046   20671
@@@

## Conclusion

So that's a 221k difference over 1000 requests, or an additional 226bytes per request leakage in development, no change in production. This is likely to be more on more complex apps, although I have not had time to do any more in depth testing.

IMHO fixing leakage in this specific way in development mode is kind of pointless, given that the exiting leakage by far out weighs it.

Or taken another way, if the inverse of the patch were proposed, I would object to it, as it would cause the issues we are seeing now.

Opinions please?</body>
      <body-html>&lt;div&gt;&lt;p&gt;So I did a little memory profiling, it was a little hacky, so my
methods may be bad. If this is so, tell me how to do it better.&lt;/p&gt;
&lt;p&gt;I restarted the server each time, and switched branches when
appropriate.&lt;/p&gt;
&lt;h2&gt;ruby -v&lt;/h2&gt;
&lt;p&gt;ruby 1.8.6 (2008-03-03 patchlevel 114) [i686-darwin9.4.0]&lt;/p&gt;
&lt;p&gt;(I ran the test against 1.8.7, freshly downloaded today, and the
result were almost identical, 1.9{,.1} fail to start due to
&lt;a href=&quot;/projects/8994/tickets/827&quot; title=&quot;Ticket #827&quot;&gt;#827&lt;/a&gt;
on 1.9 and some other issue that ran over the end of my lunch hour
on 1.9.1)&lt;/p&gt;
&lt;h2&gt;Test Script&lt;/h2&gt;


&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;
#!/usr/bin/env ruby

pid = `ps | grep script/serve[r] | awk '{ print $1 }'`.chomp
puts &amp;quot;Server PID: #{pid}&amp;quot;

url = ARGV.last
puts &amp;quot;URL: #{url}&amp;quot;

puts &amp;quot;Hit enter to go.&amp;quot;
$stdin.gets

def get_memory_usage(pid)
  `ps -o rss,vsz -p#{pid} | tail -n1`.chomp
end

puts &amp;quot;Initial Memory Usage: #{get_memory_usage(pid)}&amp;quot;

1000.times do
  system %Q{wget -O/dev/null -o/dev/null #{url}}
end

puts &amp;quot;Final Memory Usage: #{get_memory_usage(pid)}&amp;quot;

&lt;/code&gt;&lt;/pre&gt;
&lt;h2&gt;Without Patch&lt;/h2&gt;


&lt;pre&gt;&lt;code&gt;
                        RSS      VSZ
Initial Memory Usage:  31568   107692
Final Memory Usage:    43400   128408
Delta:                 11832   20716

Initial Memory Usage:  31568   107692
Final Memory Usage:    43460   128628
Delta:                 11892   20936

Initial Memory Usage:  31568   107692
Final Memory Usage:    43320   128248
Delta:                 11752   20556

Mean Delta:            11825   20736

## With Patch

Initial Memory Usage:  31568   107692
Final Memory Usage:    43584   128308
Delta:                 12016   20616

Initial Memory Usage:  31564   107692
Final Memory Usage:    43656   128468
Delta:                 12092   20776

Initial Memory Usage:  31612   107736
Final Memory Usage:    43644   128356
Delta:                 12032   20620

Mean Delta:            12046   20671
&lt;/code&gt;&lt;/pre&gt;
&lt;h2&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;So that's a 221k difference over 1000 requests, or an additional
226bytes per request leakage in development, no change in
production. This is likely to be more on more complex apps,
although I have not had time to do any more in depth testing.&lt;/p&gt;
&lt;p&gt;IMHO fixing leakage in this specific way in development mode is
kind of pointless, given that the exiting leakage by far out weighs
it.&lt;/p&gt;
&lt;p&gt;Or taken another way, if the inverse of the patch were proposed,
I would object to it, as it would cause the issues we are seeing
now.&lt;/p&gt;
&lt;p&gt;Opinions please?&lt;/p&gt;&lt;/div&gt;</body-html>
      <closed type="boolean">false</closed>
      <created-at type="datetime">2008-11-12T19:24:10+00:00</created-at>
      <creator-id type="integer">28420</creator-id>
      <diffable-attributes type="yaml">--- {}

</diffable-attributes>
      <milestone-id type="integer">9903</milestone-id>
      <number type="integer">1339</number>
      <permalink>arbase-should-not-be-nuking-its-children-just-because-it-lost-interest</permalink>
      <priority type="integer">0</priority>
      <project-id type="integer">8994</project-id>
      <state>open</state>
      <tag>activerecord bug reload</tag>
      <title>AR::Base should not be nuking it's children, just because it lost interest</title>
      <updated-at type="datetime">2008-11-12T19:24:15+00:00</updated-at>
      <user-id type="integer">28420</user-id>
      <user-name>Tom Lea</user-name>
      <creator-name>Tom Lea</creator-name>
      <url>http://rails.lighthouseapp.com/projects/8994/tickets/1339</url>
      <milestone-title>2.x</milestone-title>
      <assigned-user-name>Michael Koziarski</assigned-user-name>
    </version>
    <version type="Ticket::Version">
      <assigned-user-id type="integer">85</assigned-user-id>
      <attachments-count type="integer">1</attachments-count>
      <body>Moving to 2.2 so we can make a final call on this.

Jeremy, You've done a lot of our memory profiling lately, what's your take?</body>
      <body-html>&lt;div&gt;&lt;p&gt;Moving to 2.2 so we can make a final call on this.&lt;/p&gt;
&lt;p&gt;Jeremy, You've done a lot of our memory profiling lately, what's
your take?&lt;/p&gt;&lt;/div&gt;</body-html>
      <closed type="boolean">false</closed>
      <created-at type="datetime">2008-11-13T09:39:48+00:00</created-at>
      <creator-id type="integer">28420</creator-id>
      <diffable-attributes type="yaml">--- 
:assigned_user: 141
:milestone: 9903
</diffable-attributes>
      <milestone-id type="integer" nil="true"></milestone-id>
      <number type="integer">1339</number>
      <permalink>arbase-should-not-be-nuking-its-children-just-because-it-lost-interest</permalink>
      <priority type="integer">0</priority>
      <project-id type="integer">8994</project-id>
      <state>open</state>
      <tag>activerecord bug reload</tag>
      <title>AR::Base should not be nuking it's children, just because it lost interest</title>
      <updated-at type="datetime">2008-11-13T09:39:55+00:00</updated-at>
      <user-id type="integer">141</user-id>
      <user-name>Michael Koziarski</user-name>
      <creator-name>Tom Lea</creator-name>
      <url>http://rails.lighthouseapp.com/projects/8994/tickets/1339</url>
      <milestone-title nil="true"></milestone-title>
      <assigned-user-name>Jeremy Kemper</assigned-user-name>
    </version>
    <version type="Ticket::Version">
      <assigned-user-id type="integer">85</assigned-user-id>
      <attachments-count type="integer">1</attachments-count>
      <body>What was the application you were testing, how many AR models were involved?  It's AR which makes extensive use of define_method, especially in associations?</body>
      <body-html>&lt;div&gt;&lt;p&gt;What was the application you were testing, how many AR models
were involved? It's AR which makes extensive use of define_method,
especially in associations?&lt;/p&gt;&lt;/div&gt;</body-html>
      <closed type="boolean">false</closed>
      <created-at type="datetime">2008-11-13T09:41:27+00:00</created-at>
      <creator-id type="integer">28420</creator-id>
      <diffable-attributes type="yaml">--- {}

</diffable-attributes>
      <milestone-id type="integer" nil="true"></milestone-id>
      <number type="integer">1339</number>
      <permalink>arbase-should-not-be-nuking-its-children-just-because-it-lost-interest</permalink>
      <priority type="integer">0</priority>
      <project-id type="integer">8994</project-id>
      <state>open</state>
      <tag>activerecord bug reload</tag>
      <title>AR::Base should not be nuking it's children, just because it lost interest</title>
      <updated-at type="datetime">2008-11-13T09:41:31+00:00</updated-at>
      <user-id type="integer">141</user-id>
      <user-name>Michael Koziarski</user-name>
      <creator-name>Tom Lea</creator-name>
      <url>http://rails.lighthouseapp.com/projects/8994/tickets/1339</url>
      <milestone-title nil="true"></milestone-title>
      <assigned-user-name>Jeremy Kemper</assigned-user-name>
    </version>
    <version type="Ticket::Version">
      <assigned-user-id type="integer">85</assigned-user-id>
      <attachments-count type="integer">1</attachments-count>
      <body>Test app was [1] with a new action that just returned a blank page.

So not a very good test at all.

As mentioned, this was a bit of a botch job.

I'll try and do some more testing on a more representative app soon. Any idea what this app should involve? Or do we have a good standard test case?

[1] http://rails.lighthouseapp.com/attachments/59425/lighthouse-ticket-1290.zip</body>
      <body-html>&lt;div&gt;&lt;p&gt;Test app was &lt;a href=&quot;/projects/8994/changesets/1&quot; title=&quot;Changeset #1&quot;&gt;[1]&lt;/a&gt; with a new action that just returned a blank
page.&lt;/p&gt;
&lt;p&gt;So not a very good test at all.&lt;/p&gt;
&lt;p&gt;As mentioned, this was a bit of a botch job.&lt;/p&gt;
&lt;p&gt;I'll try and do some more testing on a more representative app
soon. Any idea what this app should involve? Or do we have a good
standard test case?&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;/projects/8994/changesets/1&quot; title=&quot;Changeset #1&quot;&gt;[1]&lt;/a&gt; &lt;a href=&quot;http://rails.lighthouseapp.com/attachments/59425/lighthouse-ticket-1290.zip&quot;&gt;
http://rails.lighthouseapp.com/a...&lt;/a&gt;&lt;/p&gt;&lt;/div&gt;</body-html>
      <closed type="boolean">false</closed>
      <created-at type="datetime">2008-11-13T09:55:01+00:00</created-at>
      <creator-id type="integer">28420</creator-id>
      <diffable-attributes type="yaml">--- {}

</diffable-attributes>
      <milestone-id type="integer" nil="true"></milestone-id>
      <number type="integer">1339</number>
      <permalink>arbase-should-not-be-nuking-its-children-just-because-it-lost-interest</permalink>
      <priority type="integer">0</priority>
      <project-id type="integer">8994</project-id>
      <state>open</state>
      <tag>activerecord bug reload</tag>
      <title>AR::Base should not be nuking it's children, just because it lost interest</title>
      <updated-at type="datetime">2008-11-13T09:55:04+00:00</updated-at>
      <user-id type="integer">28420</user-id>
      <user-name>Tom Lea</user-name>
      <creator-name>Tom Lea</creator-name>
      <url>http://rails.lighthouseapp.com/projects/8994/tickets/1339</url>
      <milestone-title nil="true"></milestone-title>
      <assigned-user-name>Jeremy Kemper</assigned-user-name>
    </version>
    <version type="Ticket::Version">
      <assigned-user-id type="integer">85</assigned-user-id>
      <attachments-count type="integer">1</attachments-count>
      <body>I think it would be great to get this fixed. But we need to make sure we're not just adding back the leaking headache. I remember before we had this and it wasn't pretty. You'd have to restart your dev server pretty often on a complex app.

Perhaps this is no longer valid, but we should absolutely confirm that before including it. So I say we don't aim for 2.2 final, but as soon as we have a confirmation, we put it in there.</body>
      <body-html>&lt;div&gt;&lt;p&gt;I think it would be great to get this fixed. But we need to make
sure we're not just adding back the leaking headache. I remember
before we had this and it wasn't pretty. You'd have to restart your
dev server pretty often on a complex app.&lt;/p&gt;
&lt;p&gt;Perhaps this is no longer valid, but we should absolutely
confirm that before including it. So I say we don't aim for 2.2
final, but as soon as we have a confirmation, we put it in
there.&lt;/p&gt;&lt;/div&gt;</body-html>
      <closed type="boolean">false</closed>
      <created-at type="datetime">2008-11-13T10:08:20+00:00</created-at>
      <creator-id type="integer">28420</creator-id>
      <diffable-attributes type="yaml">--- 
:milestone: 12671
</diffable-attributes>
      <milestone-id type="integer">9903</milestone-id>
      <number type="integer">1339</number>
      <permalink>arbase-should-not-be-nuking-its-children-just-because-it-lost-interest</permalink>
      <priority type="integer">0</priority>
      <project-id type="integer">8994</project-id>
      <state>open</state>
      <tag>activerecord bug reload</tag>
      <title>AR::Base should not be nuking it's children, just because it lost interest</title>
      <updated-at type="datetime">2008-11-13T10:08:22+00:00</updated-at>
      <user-id type="integer">83</user-id>
      <user-name>DHH</user-name>
      <creator-name>Tom Lea</creator-name>
      <url>http://rails.lighthouseapp.com/projects/8994/tickets/1339</url>
      <milestone-title>2.x</milestone-title>
      <assigned-user-name>Jeremy Kemper</assigned-user-name>
    </version>
    <version type="Ticket::Version">
      <assigned-user-id type="integer">85</assigned-user-id>
      <attachments-count type="integer">1</attachments-count>
      <body>Guys,
&lt;usual newbie apologies&gt;
I get the exact same error as described at the top of ref2 (duplicate ticket) using ActiveRecord::Base#hash. (First I do Object.const_set(name, AR_subclass then, when running AR#hash a second time - after reload - it happens.)

Can anyone help out a newcomer that don't understand the suggestions above:
- Should I apply the patch and how? Am I supposed to edit the source to try replicate the diff?
- Is there a workaround? If so, where can I find instructions? 
- If no, shouldn't this be high prio ticket, or is this weird/unusual use of RoR?
- The difficulty of understanding what is going and the impression of side-effects is making a newcomer a bit worried:
Array#uniq fails because AR#hash fails after reload because AR works differently when defining subclasses &quot;dynamically&quot; - none of which is easy to see or explained anywhere. etc etc

I use Rails 2.2.2 on jruby 1.1.5 (ruby 1.8.6 patchlevel 114) (2008-11-03 rev 7996) on Java 1.6 and win XP</body>
      <body-html>&lt;div&gt;&lt;p&gt;Guys, I get the exact same error as described at the top of ref2
(duplicate ticket) using ActiveRecord::Base#hash. (First I do
Object.const_set(name, AR_subclass then, when running AR#hash a
second time - after reload - it happens.)&lt;/p&gt;
&lt;p&gt;Can anyone help out a newcomer that don't understand the
suggestions above: - Should I apply the patch and how? Am I
supposed to edit the source to try replicate the diff? - Is there a
workaround? If so, where can I find instructions? - If no,
shouldn't this be high prio ticket, or is this weird/unusual use of
RoR? - The difficulty of understanding what is going and the
impression of side-effects is making a newcomer a bit worried:
Array#uniq fails because AR#hash fails after reload because AR
works differently when defining subclasses &quot;dynamically&quot; - none of
which is easy to see or explained anywhere. etc etc&lt;/p&gt;
&lt;p&gt;I use Rails 2.2.2 on jruby 1.1.5 (ruby 1.8.6 patchlevel 114)
(2008-11-03 rev 7996) on Java 1.6 and win XP&lt;/p&gt;&lt;/div&gt;</body-html>
      <closed type="boolean">false</closed>
      <created-at type="datetime">2008-12-07T00:17:01+00:00</created-at>
      <creator-id type="integer">28420</creator-id>
      <diffable-attributes type="yaml">--- {}

</diffable-attributes>
      <milestone-id type="integer">9903</milestone-id>
      <number type="integer">1339</number>
      <permalink>arbase-should-not-be-nuking-its-children-just-because-it-lost-interest</permalink>
      <priority type="integer">0</priority>
      <project-id type="integer">8994</project-id>
      <state>open</state>
      <tag>activerecord bug reload</tag>
      <title>AR::Base should not be nuking it's children, just because it lost interest</title>
      <updated-at type="datetime">2008-12-07T00:17:02+00:00</updated-at>
      <user-id type="integer">40008</user-id>
      <user-name>Gunnar Djurberg</user-name>
      <creator-name>Tom Lea</creator-name>
      <url>http://rails.lighthouseapp.com/projects/8994/tickets/1339</url>
      <milestone-title>2.x</milestone-title>
      <assigned-user-name>Jeremy Kemper</assigned-user-name>
    </version>
    <version type="Ticket::Version">
      <assigned-user-id type="integer">85</assigned-user-id>
      <attachments-count type="integer">1</attachments-count>
      <body>Having encountered this problem myself (and being stuck on it repeateadly) I have investigated the error (and hopefully found a good fix). Here's what I know about it:

1. It happens when ActiveRecord::Base#reset_subclasses is called by the dispatcher between requests (in dev mode only).
2. ActiveRecord::Base#reset_subclasses wipes out the inheritable_attributes Hash (where #skip_time_zone_conversion_for_attributes is stored).
3. It will not only happen on objects persisted through requests, as the &quot;monkey test app&quot; from #1290 shows, but also when trying to access generated association methods on AR, even for objects that live only on the current request.
4. This bug was introduced by [this commit](http://github.com/rails/rails/commit/9c4730d01e892df8d5c5493a08e0cddf0de5d575) where the #skip_time_zone_conversion_for_attributes declaration was changed from base.cattr_accessor to base.class_inheritable_accessor. But then again, that same commit also fixed something else.
5. The patch initially submitted here that simply avoids clearing the instance_variables and instance_methods in reset_subclasses does introduce massive leaking, and the amounts leaked seem directly proportional to complexity of the app (i.e. number of models, associations and attributes on each of them). I have a pretty complex app which leaks nearly 1Mb on each request in dev mode when the patch is applied. So it's not viable (for me anyways).
6. While trying out different ways to solve this, I have corrected the initial error (skip_time_zone_conversion_for_attributes being nil on 2nd request), but it uncovered another error (which just didn't happen because the first exception would be raised before getting to it). That error seems to be the one reported in #774 (Stack overflow in method_missing for the 'id' method).

Now, for the solution, my patch (attached) does the following:

1. It adds wrapper methods for #skip_time_zone_conversion_for_attributes methods, making sure it always reads/writes the value as an class_inheritable_attribute. This way, nil is never returned anymore.
2. It ensures that the 'id' method is not wiped out when reset_subclasses is called. AR is kinda strange on that one, because it first defines it directly in the source, but redefines itself with #define_read_method when it is first called. And that is precisely what makes it fail after reloading (since reset_subclasses then wipes it out).

I also added a test in reload_models_test.rb, which calls reset_subclasses to try and simulate reloading between requests in dev mode. What I cannot tell at this point is if it really triggers the reloading mechanism as it does on a live dispatcher request cycle. I also tested from script/server and the error was gone.

Comments, opinions (and probably further testing) are welcome.

Regards,</body>
      <body-html>&lt;div&gt;&lt;p&gt;Having encountered this problem myself (and being stuck on it
repeateadly) I have investigated the error (and hopefully found a
good fix). Here's what I know about it:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;It happens when ActiveRecord::Base#reset_subclasses is called
by the dispatcher between requests (in dev mode only).&lt;/li&gt;
&lt;li&gt;ActiveRecord::Base#reset_subclasses wipes out the
inheritable_attributes Hash (where
#skip_time_zone_conversion_for_attributes is stored).&lt;/li&gt;
&lt;li&gt;It will not only happen on objects persisted through requests,
as the &quot;monkey test app&quot; from &lt;a href=&quot;/projects/8994/tickets/1290&quot; title=&quot;Ticket #1290&quot;&gt;#1290&lt;/a&gt; shows, but also when trying to
access generated association methods on AR, even for objects that
live only on the current request.&lt;/li&gt;
&lt;li&gt;This bug was introduced by &lt;a href=&quot;http://github.com/rails/rails/commit/9c4730d01e892df8d5c5493a08e0cddf0de5d575&quot;&gt;
this commit&lt;/a&gt; where the #skip_time_zone_conversion_for_attributes
declaration was changed from base.cattr_accessor to
base.class_inheritable_accessor. But then again, that same commit
also fixed something else.&lt;/li&gt;
&lt;li&gt;The patch initially submitted here that simply avoids clearing
the instance_variables and instance_methods in reset_subclasses
does introduce massive leaking, and the amounts leaked seem
directly proportional to complexity of the app (i.e. number of
models, associations and attributes on each of them). I have a
pretty complex app which leaks nearly 1Mb on each request in dev
mode when the patch is applied. So it's not viable (for me
anyways).&lt;/li&gt;
&lt;li&gt;While trying out different ways to solve this, I have corrected
the initial error (skip_time_zone_conversion_for_attributes being
nil on 2nd request), but it uncovered another error (which just
didn't happen because the first exception would be raised before
getting to it). That error seems to be the one reported in &lt;a href=&quot;/projects/8994/tickets/774&quot; title=&quot;Ticket #774&quot;&gt;#774&lt;/a&gt; (Stack
overflow in method_missing for the 'id' method).&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Now, for the solution, my patch (attached) does the
following:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;It adds wrapper methods for
#skip_time_zone_conversion_for_attributes methods, making sure it
always reads/writes the value as an class_inheritable_attribute.
This way, nil is never returned anymore.&lt;/li&gt;
&lt;li&gt;It ensures that the 'id' method is not wiped out when
reset_subclasses is called. AR is kinda strange on that one,
because it first defines it directly in the source, but redefines
itself with #define_read_method when it is first called. And that
is precisely what makes it fail after reloading (since
reset_subclasses then wipes it out).&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;I also added a test in reload_models_test.rb, which calls
reset_subclasses to try and simulate reloading between requests in
dev mode. What I cannot tell at this point is if it really triggers
the reloading mechanism as it does on a live dispatcher request
cycle. I also tested from script/server and the error was gone.&lt;/p&gt;
&lt;p&gt;Comments, opinions (and probably further testing) are
welcome.&lt;/p&gt;
&lt;p&gt;Regards,&lt;/p&gt;&lt;/div&gt;</body-html>
      <closed type="boolean">false</closed>
      <created-at type="datetime">2009-01-06T19:13:49+00:00</created-at>
      <creator-id type="integer">28420</creator-id>
      <diffable-attributes type="yaml">--- {}

</diffable-attributes>
      <milestone-id type="integer">9903</milestone-id>
      <number type="integer">1339</number>
      <permalink>arbase-should-not-be-nuking-its-children-just-because-it-lost-interest</permalink>
      <priority type="integer">0</priority>
      <project-id type="integer">8994</project-id>
      <state>open</state>
      <tag>activerecord bug reload</tag>
      <title>AR::Base should not be nuking it's children, just because it lost interest</title>
      <updated-at type="datetime">2009-01-06T19:13:53+00:00</updated-at>
      <user-id type="integer">42669</user-id>
      <user-name>C. Bedard</user-name>
      <creator-name>Tom Lea</creator-name>
      <url>http://rails.lighthouseapp.com/projects/8994/tickets/1339</url>
      <milestone-title>2.x</milestone-title>
      <assigned-user-name>Jeremy Kemper</assigned-user-name>
    </version>
    <version type="Ticket::Version">
      <assigned-user-id type="integer">85</assigned-user-id>
      <attachments-count type="integer">2</attachments-count>
      <body>I think it's fine to add getter and setter wrapper methods around skip_time_zone_conversion_for_attributes -- this would remove the need to use an explicit +self+ receiver whenever you need to declare these attributes in a model -- and it does address *some* of the issues found with calling methods on cached models: i.e., you can now call method_missing-defined methods without failure.

But keep in mind, with or without this fix, there's still a problem with user-defined methods on a model being removed -- in the Monkey app, the Monkey model defines a #&gt; method (it could be named #foo or whatever) and this method fails on the second page refresh, with or without a skip_time_zone_ fix.</body>
      <body-html>&lt;div&gt;&lt;p&gt;I think it's fine to add getter and setter wrapper methods
around skip_time_zone_conversion_for_attributes -- this would
remove the need to use an explicit +self+ receiver whenever you
need to declare these attributes in a model -- and it does address
&lt;em&gt;some&lt;/em&gt; of the issues found with calling methods on cached
models: i.e., you can now call method_missing-defined methods
without failure.&lt;/p&gt;
&lt;p&gt;But keep in mind, with or without this fix, there's still a
problem with user-defined methods on a model being removed -- in
the Monkey app, the Monkey model defines a #&amp;gt; method (it could
be named #foo or whatever) and this method fails on the second page
refresh, with or without a skip_time_zone_ fix.&lt;/p&gt;&lt;/div&gt;</body-html>
      <closed type="boolean">false</closed>
      <created-at type="datetime">2009-01-07T05:59:47+00:00</created-at>
      <creator-id type="integer">28420</creator-id>
      <diffable-attributes type="yaml">--- {}

</diffable-attributes>
      <milestone-id type="integer">9903</milestone-id>
      <number type="integer">1339</number>
      <permalink>arbase-should-not-be-nuking-its-children-just-because-it-lost-interest</permalink>
      <priority type="integer">0</priority>
      <project-id type="integer">8994</project-id>
      <state>open</state>
      <tag>activerecord bug reload</tag>
      <title>AR::Base should not be nuking it's children, just because it lost interest</title>
      <updated-at type="datetime">2009-01-07T05:59:52+00:00</updated-at>
      <user-id type="integer">15783</user-id>
      <user-name>Geoff Buesing</user-name>
      <creator-name>Tom Lea</creator-name>
      <url>http://rails.lighthouseapp.com/projects/8994/tickets/1339</url>
      <milestone-title>2.x</milestone-title>
      <assigned-user-name>Jeremy Kemper</assigned-user-name>
    </version>
    <version type="Ticket::Version">
      <assigned-user-id type="integer">85</assigned-user-id>
      <attachments-count type="integer">2</attachments-count>
      <body>I get the same error too, only in development mode. 

Environment:  Mac OSX 10.5, Rails 2.2.2, Mongrel 1.1.5, ruby 1.8.6 (2008-03-03 patchlevel 114)

In my case, the error appears when the app call a observer. I fix it  changing &quot;config.time_zone = 'Madrid'&quot; for &quot;config.active_record.default_timezone = 'Madrid'&quot; in environment.rb

In my case the solution proposed by Geoff in  #1290 doesn't work.</body>
      <body-html>&lt;div&gt;&lt;p&gt;I get the same error too, only in development mode.&lt;/p&gt;
&lt;p&gt;Environment: Mac OSX 10.5, Rails 2.2.2, Mongrel 1.1.5, ruby
1.8.6 (2008-03-03 patchlevel 114)&lt;/p&gt;
&lt;p&gt;In my case, the error appears when the app call a observer. I
fix it changing &quot;config.time_zone = 'Madrid'&quot; for
&quot;config.active_record.default_timezone = 'Madrid'&quot; in
environment.rb&lt;/p&gt;
&lt;p&gt;In my case the solution proposed by Geoff in &lt;a href=&quot;/projects/8994/tickets/1290&quot; title=&quot;Ticket #1290&quot;&gt;#1290&lt;/a&gt;
doesn't work.&lt;/p&gt;&lt;/div&gt;</body-html>
      <closed type="boolean">false</closed>
      <created-at type="datetime">2009-01-07T08:20:48+00:00</created-at>
      <creator-id type="integer">28420</creator-id>
      <diffable-attributes type="yaml">--- {}

</diffable-attributes>
      <milestone-id type="integer">9903</milestone-id>
      <number type="integer">1339</number>
      <permalink>arbase-should-not-be-nuking-its-children-just-because-it-lost-interest</permalink>
      <priority type="integer">0</priority>
      <project-id type="integer">8994</project-id>
      <state>open</state>
      <tag>activerecord bug reload</tag>
      <title>AR::Base should not be nuking it's children, just because it lost interest</title>
      <updated-at type="datetime">2009-01-07T08:20:51+00:00</updated-at>
      <user-id type="integer">33400</user-id>
      <user-name>Emili Parre&#241;o</user-name>
      <creator-name>Tom Lea</creator-name>
      <url>http://rails.lighthouseapp.com/projects/8994/tickets/1339</url>
      <milestone-title>2.x</milestone-title>
      <assigned-user-name>Jeremy Kemper</assigned-user-name>
    </version>
    <version type="Ticket::Version">
      <assigned-user-id type="integer">85</assigned-user-id>
      <attachments-count type="integer">2</attachments-count>
      <body>In my view, the Monkey app, while it does produce our error, uses the Rails framework in a specific way that is more related to persistence across requests than standard use of an AR instance.

The intended and expected behavior of the framework is to allow to create, use and dispose of an AR object within a single request (and repeat that consistently on each request).

I'm not saying that Rails shouldn't provide functionality to allow to do what the Monkey app does (which it already does if serialization was used), but that would represent an addition to the framework (as opposed to currently being a bug on expected behavior). 

It would be very nice to come up with a way that addresses both problems at once, but the way the reloading works does not allow that, because it creates *new* class instances on each request, instead of stripping a class instance from its methods and attributes and re-applying them to the same instance.

In the case of the Monkey app, if MONKEY_ONE is to be initialized that early and be kept throughout the life of the application, auto-reloading can (and should) be avoided with a simple explicit require (in extensions.rb) :

@@@ruby
require File.join(RAILS_ROOT,'app/models/monkey')

MONKEY_ONE = Monkey.first
@@@

And now the Monkey is not so amnesic anymore ;-)

`ActiveSupport::Dependencies` only autoloads missing classes (constants) and the dispatcher only unloads the autoloaded classes in `#cleanup_application`. This is why, for example, classes defined in plugins do not unload automatically.

Of course, there may be other issues with reloading that we are not aware of, and I'll be glad to help if anything else comes up.</body>
      <body-html>&lt;div&gt;&lt;p&gt;In my view, the Monkey app, while it does produce our error,
uses the Rails framework in a specific way that is more related to
persistence across requests than standard use of an AR
instance.&lt;/p&gt;
&lt;p&gt;The intended and expected behavior of the framework is to allow
to create, use and dispose of an AR object within a single request
(and repeat that consistently on each request).&lt;/p&gt;
&lt;p&gt;I'm not saying that Rails shouldn't provide functionality to
allow to do what the Monkey app does (which it already does if
serialization was used), but that would represent an addition to
the framework (as opposed to currently being a bug on expected
behavior).&lt;/p&gt;
&lt;p&gt;It would be very nice to come up with a way that addresses both
problems at once, but the way the reloading works does not allow
that, because it creates &lt;em&gt;new&lt;/em&gt; class instances on each
request, instead of stripping a class instance from its methods and
attributes and re-applying them to the same instance.&lt;/p&gt;
&lt;p&gt;In the case of the Monkey app, if MONKEY_ONE is to be
initialized that early and be kept throughout the life of the
application, auto-reloading can (and should) be avoided with a
simple explicit require (in extensions.rb) :&lt;/p&gt;
&lt;p&gt;@@@ruby require File.join(RAILS_ROOT,'app/models/monkey')&lt;/p&gt;
&lt;p&gt;MONKEY_ONE = Monkey.first&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;

And now the Monkey is not so amnesic anymore ;-)

`ActiveSupport::Dependencies` only autoloads missing classes (constants) and the dispatcher only unloads the autoloaded classes in `#cleanup_application`. This is why, for example, classes defined in plugins do not unload automatically.

Of course, there may be other issues with reloading that we are not aware of, and I'll be glad to help if anything else comes up.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;</body-html>
      <closed type="boolean">false</closed>
      <created-at type="datetime">2009-01-07T15:52:33+00:00</created-at>
      <creator-id type="integer">28420</creator-id>
      <diffable-attributes type="yaml">--- {}

</diffable-attributes>
      <milestone-id type="integer">9903</milestone-id>
      <number type="integer">1339</number>
      <permalink>arbase-should-not-be-nuking-its-children-just-because-it-lost-interest</permalink>
      <priority type="integer">0</priority>
      <project-id type="integer">8994</project-id>
      <state>open</state>
      <tag>activerecord bug reload</tag>
      <title>AR::Base should not be nuking it's children, just because it lost interest</title>
      <updated-at type="datetime">2009-01-07T15:52:36+00:00</updated-at>
      <user-id type="integer">42669</user-id>
      <user-name>C. Bedard</user-name>
      <creator-name>Tom Lea</creator-name>
      <url>http://rails.lighthouseapp.com/projects/8994/tickets/1339</url>
      <milestone-title>2.x</milestone-title>
      <assigned-user-name>Jeremy Kemper</assigned-user-name>
    </version>
    <version type="Ticket::Version">
      <assigned-user-id type="integer">85</assigned-user-id>
      <attachments-count type="integer">2</attachments-count>
      <body>Sorry about the poor formatting in my previous message...(A preview function would be nice in here :-S ).</body>
      <body-html>&lt;div&gt;&lt;p&gt;Sorry about the poor formatting in my previous message...(A
preview function would be nice in here :-S ).&lt;/p&gt;&lt;/div&gt;</body-html>
      <closed type="boolean">false</closed>
      <created-at type="datetime">2009-01-07T15:56:49+00:00</created-at>
      <creator-id type="integer">28420</creator-id>
      <diffable-attributes type="yaml">--- {}

</diffable-attributes>
      <milestone-id type="integer">9903</milestone-id>
      <number type="integer">1339</number>
      <permalink>arbase-should-not-be-nuking-its-children-just-because-it-lost-interest</permalink>
      <priority type="integer">0</priority>
      <project-id type="integer">8994</project-id>
      <state>open</state>
      <tag>activerecord bug reload</tag>
      <title>AR::Base should not be nuking it's children, just because it lost interest</title>
      <updated-at type="datetime">2009-01-07T15:56:52+00:00</updated-at>
      <user-id type="integer">42669</user-id>
      <user-name>C. Bedard</user-name>
      <creator-name>Tom Lea</creator-name>
      <url>http://rails.lighthouseapp.com/projects/8994/tickets/1339</url>
      <milestone-title>2.x</milestone-title>
      <assigned-user-name>Jeremy Kemper</assigned-user-name>
    </version>
    <version type="Ticket::Version">
      <assigned-user-id type="integer">85</assigned-user-id>
      <attachments-count type="integer">2</attachments-count>
      <body>@Emili -- config.active_record.default_timezone represents the zone that times are persisted in -- you can only set this to :utc or :local; 'Madrid' is not a valid setting. If you're using config.time_zone, you don't need to set active_record.default_timezone, because this will automatically set it to :utc for you.

With config.time_zone = 'Madrid', and with no explicit active_record.default_timezone setting, are you still experiencing issues?

@C. Bedard -- one thing that's not clear for me: in your previous comment, you refer to an issue &quot;when trying to access generated association methods on AR, even for objects that live only on the current request&quot; Do we have an example of this issue?</body>
      <body-html>&lt;div&gt;&lt;p&gt;@Emili -- config.active_record.default_timezone represents the
zone that times are persisted in -- you can only set this to :utc
or :local; 'Madrid' is not a valid setting. If you're using
config.time_zone, you don't need to set
active_record.default_timezone, because this will automatically set
it to :utc for you.&lt;/p&gt;
&lt;p&gt;With config.time_zone = 'Madrid', and with no explicit
active_record.default_timezone setting, are you still experiencing
issues?&lt;/p&gt;
&lt;p&gt;@C. Bedard -- one thing that's not clear for me: in your
previous comment, you refer to an issue &quot;when trying to access
generated association methods on AR, even for objects that live
only on the current request&quot; Do we have an example of this
issue?&lt;/p&gt;&lt;/div&gt;</body-html>
      <closed type="boolean">false</closed>
      <created-at type="datetime">2009-01-07T18:01:45+00:00</created-at>
      <creator-id type="integer">28420</creator-id>
      <diffable-attributes type="yaml">--- {}

</diffable-attributes>
      <milestone-id type="integer">9903</milestone-id>
      <number type="integer">1339</number>
      <permalink>arbase-should-not-be-nuking-its-children-just-because-it-lost-interest</permalink>
      <priority type="integer">0</priority>
      <project-id type="integer">8994</project-id>
      <state>open</state>
      <tag>activerecord bug reload</tag>
      <title>AR::Base should not be nuking it's children, just because it lost interest</title>
      <updated-at type="datetime">2009-01-07T18:01:50+00:00</updated-at>
      <user-id type="integer">15783</user-id>
      <user-name>Geoff Buesing</user-name>
      <creator-name>Tom Lea</creator-name>
      <url>http://rails.lighthouseapp.com/projects/8994/tickets/1339</url>
      <milestone-title>2.x</milestone-title>
      <assigned-user-name>Jeremy Kemper</assigned-user-name>
    </version>
    <version type="Ticket::Version">
      <assigned-user-id type="integer">85</assigned-user-id>
      <attachments-count type="integer">2</attachments-count>
      <body>@Geoff: 

I have made an example (attached) that triggers the error without persisting AR objects between requests. I try and explain as cleary as possible what goes on (from what I understand) in the app README.

Basically what happens is that a file that is explicitely required (with `require 'file'`), it will not be reloaded on each request, while the class it declares is being wiped out by ActiveRecord::Base#reset_subclasses. On 2nd request, the class remain &quot;empty&quot;...

Obviously, the whole thing can be avoided by never using `require` in files that declare AR subclasses. But there can be contexts in which explicit requires are needed (I have an app for which it is the case).</body>
      <body-html>&lt;div&gt;&lt;p&gt;@Geoff:&lt;/p&gt;
&lt;p&gt;I have made an example (attached) that triggers the error
without persisting AR objects between requests. I try and explain
as cleary as possible what goes on (from what I understand) in the
app README.&lt;/p&gt;
&lt;p&gt;Basically what happens is that a file that is explicitely
required (with &lt;code&gt;require 'file'&lt;/code&gt;), it will not be
reloaded on each request, while the class it declares is being
wiped out by ActiveRecord::Base#reset_subclasses. On 2nd request,
the class remain &quot;empty&quot;...&lt;/p&gt;
&lt;p&gt;Obviously, the whole thing can be avoided by never using
&lt;code&gt;require&lt;/code&gt; in files that declare AR subclasses. But there
can be contexts in which explicit requires are needed (I have an
app for which it is the case).&lt;/p&gt;&lt;/div&gt;</body-html>
      <closed type="boolean">false</closed>
      <created-at type="datetime">2009-01-08T05:36:24+00:00</created-at>
      <creator-id type="integer">28420</creator-id>
      <diffable-attributes type="yaml">--- 
:tag: activerecord bug reload
</diffable-attributes>
      <milestone-id type="integer">9903</milestone-id>
      <number type="integer">1339</number>
      <permalink>arbase-should-not-be-nuking-its-children-just-because-it-lost-interest</permalink>
      <priority type="integer">0</priority>
      <project-id type="integer">8994</project-id>
      <state>open</state>
      <tag>activerecord bug dependencies reload</tag>
      <title>AR::Base should not be nuking it's children, just because it lost interest</title>
      <updated-at type="datetime">2009-01-08T05:36:29+00:00</updated-at>
      <user-id type="integer">42669</user-id>
      <user-name>C. Bedard</user-name>
      <creator-name>Tom Lea</creator-name>
      <url>http://rails.lighthouseapp.com/projects/8994/tickets/1339</url>
      <milestone-title>2.x</milestone-title>
      <assigned-user-name>Jeremy Kemper</assigned-user-name>
    </version>
    <version type="Ticket::Version">
      <assigned-user-id type="integer">85</assigned-user-id>
      <attachments-count type="integer">3</attachments-count>
      <body>@C. Bedard -- thanks for the example, very helpful to have this. I think you're right about the explicit require mucking up the works with Dependencies and class reloading. 

I did some experiments with the app, here's what I found:

1. with the require for another AR-model declared at the top of Monkey, you get an exception on the second request (as promised.) The stack trace stops at a NoMethodError for skip_time_zone_

2. *without* the require, things work fine (again, as promised.)

3. if I move the user_role require from Monkey to the bottom of environment.rb, I experience the same issue on the second request.

4. *with* the require, but if I add this method to each of the models:

@@@ ruby
def self.skip_time_zone_conversion_for_attributes
  []
end
@@@

things work fine. This is similar to what your patch does, i.e., work around the class inheritable accessor issue with skip_time_zone_.

HOWEVER:

5. *with* the require, and *with* skip_time_zone_ explicitly declared as a class method, if I declare a method #foo in Role, and then I change this line in the view from:

@@@ ruby
# calling a Role attribute method defined by method_missing:
u.user_roles.collect { |ur| ur.role.description }.join(&quot;, &quot;)
@@@

to

@@@ ruby
# calling a method explicitly defined in Role class:
u.user_roles.collect { |ur| ur.role.foo }.join(&quot;, &quot;)
@@@

...I get a NoMethodError on the *second* request -- the stack trace tops out at method_missing.

So, it seems like this reload issue is very similar to the issue with objects cached between requests -- there's the class inheritable accessor issue with skip_time_zone_, but even if you work around that, there are issues with calling methods that aren't regenerated by method_missing.

Possible solutions for these problems:

1. for the issue with objects cached between requests -- we can instruct users to always use Rails.cache with a marshaling memory store

2. for the explicit require issue -- we can instruct users to never explicitly require an AR model when reloading is desired, as you suggest.</body>
      <body-html>&lt;div&gt;&lt;p&gt;@C. Bedard -- thanks for the example, very helpful to have this.
I think you're right about the explicit require mucking up the
works with Dependencies and class reloading.&lt;/p&gt;
&lt;p&gt;I did some experiments with the app, here's what I found:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;with the require for another AR-model declared at the top of
Monkey, you get an exception on the second request (as promised.)
The stack trace stops at a NoMethodError for skip_time_zone_&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;em&gt;without&lt;/em&gt; the require, things work fine (again, as
promised.)&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;if I move the user_role require from Monkey to the bottom of
environment.rb, I experience the same issue on the second
request.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;em&gt;with&lt;/em&gt; the require, but if I add this method to each of
the models:&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;


&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;
def self.skip_time_zone_conversion_for_attributes
  []
end
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;things work fine. This is similar to what your patch does, i.e.,
work around the class inheritable accessor issue with
skip_time_zone_.&lt;/p&gt;
&lt;p&gt;HOWEVER:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;em&gt;with&lt;/em&gt; the require, and &lt;em&gt;with&lt;/em&gt; skip_time_zone_
explicitly declared as a class method, if I declare a method #foo
in Role, and then I change this line in the view from:&lt;/li&gt;
&lt;/ol&gt;


&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;
# calling a Role attribute method defined by method_missing:
u.user_roles.collect { |ur| ur.role.description }.join(&amp;quot;, &amp;quot;)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;to&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;
# calling a method explicitly defined in Role class:
u.user_roles.collect { |ur| ur.role.foo }.join(&amp;quot;, &amp;quot;)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;...I get a NoMethodError on the &lt;em&gt;second&lt;/em&gt; request -- the
stack trace tops out at method_missing.&lt;/p&gt;
&lt;p&gt;So, it seems like this reload issue is very similar to the issue
with objects cached between requests -- there's the class
inheritable accessor issue with skip_time_zone_, but even if you
work around that, there are issues with calling methods that aren't
regenerated by method_missing.&lt;/p&gt;
&lt;p&gt;Possible solutions for these problems:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;for the issue with objects cached between requests -- we can
instruct users to always use Rails.cache with a marshaling memory
store&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;for the explicit require issue -- we can instruct users to never
explicitly require an AR model when reloading is desired, as you
suggest.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;&lt;/div&gt;</body-html>
      <closed type="boolean">false</closed>
      <created-at type="datetime">2009-01-08T15:44:52+00:00</created-at>
      <creator-id type="integer">28420</creator-id>
      <diffable-attributes type="yaml">--- {}

</diffable-attributes>
      <milestone-id type="integer">9903</milestone-id>
      <number type="integer">1339</number>
      <permalink>arbase-should-not-be-nuking-its-children-just-because-it-lost-interest</permalink>
      <priority type="integer">0</priority>
      <project-id type="integer">8994</project-id>
      <state>open</state>
      <tag>activerecord bug dependencies reload</tag>
      <title>AR::Base should not be nuking it's children, just because it lost interest</title>
      <updated-at type="datetime">2009-01-08T15:44:57+00:00</updated-at>
      <user-id type="integer">15783</user-id>
      <user-name>Geoff Buesing</user-name>
      <creator-name>Tom Lea</creator-name>
      <url>http://rails.lighthouseapp.com/projects/8994/tickets/1339</url>
      <milestone-title>2.x</milestone-title>
      <assigned-user-name>Jeremy Kemper</assigned-user-name>
    </version>
    <version type="Ticket::Version">
      <assigned-user-id type="integer">85</assigned-user-id>
      <attachments-count type="integer">3</attachments-count>
      <body>@C. Bedard -- in regards to your patch: by working around issues with #id and #skip_time_zone_, you may just be pushing the problem further downstream, instead of truly fixing things.</body>
      <body-html>&lt;div&gt;&lt;p&gt;@C. Bedard -- in regards to your patch: by working around issues
with #id and #skip_time_zone_, you may just be pushing the problem
further downstream, instead of truly fixing things.&lt;/p&gt;&lt;/div&gt;</body-html>
      <closed type="boolean">false</closed>
      <created-at type="datetime">2009-01-08T16:05:23+00:00</created-at>
      <creator-id type="integer">28420</creator-id>
      <diffable-attributes type="yaml">--- {}

</diffable-attributes>
      <milestone-id type="integer">9903</milestone-id>
      <number type="integer">1339</number>
      <permalink>arbase-should-not-be-nuking-its-children-just-because-it-lost-interest</permalink>
      <priority type="integer">0</priority>
      <project-id type="integer">8994</project-id>
      <state>open</state>
      <tag>activerecord bug dependencies reload</tag>
      <title>AR::Base should not be nuking it's children, just because it lost interest</title>
      <updated-at type="datetime">2009-01-08T16:05:27+00:00</updated-at>
      <user-id type="integer">15783</user-id>
      <user-name>Geoff Buesing</user-name>
      <creator-name>Tom Lea</creator-name>
      <url>http://rails.lighthouseapp.com/projects/8994/tickets/1339</url>
      <milestone-title>2.x</milestone-title>
      <assigned-user-name>Jeremy Kemper</assigned-user-name>
    </version>
    <version type="Ticket::Version">
      <assigned-user-id type="integer">85</assigned-user-id>
      <attachments-count type="integer">3</attachments-count>
      <body>@Geoff:

Indeed my patch doesn't solve the problem. I didn't think at that time that it was related to dependencies. 

If `skip_time_zone_for_attributes` actually contained some fields, they would be cleared after 1st request and since the class would never reload on 2nd request, it would be silently reset to an empty array. Same goes for any other method (my patch actually dealt with the `id` method also, but that was a short-sighted fix since all methods are undef'ed).

And I would add to your second suggestion:

*If* an explicit `require` is needed, then those model files should be loaded on app initialization and users would need to understand they lose the reloading functionality for those models in dev mode.

As for you suggestion to persist objects to the Rails.cache, I am not too familiar on how it works, but how would an object cope with being tied to a Class that has been stripped from its method declarations? 
Does the Class instance get cached too? And if so, isn't it the same as not reloading it?</body>
      <body-html>&lt;div&gt;&lt;p&gt;@Geoff:&lt;/p&gt;
&lt;p&gt;Indeed my patch doesn't solve the problem. I didn't think at
that time that it was related to dependencies.&lt;/p&gt;
&lt;p&gt;If &lt;code&gt;skip_time_zone_for_attributes&lt;/code&gt; actually contained
some fields, they would be cleared after 1st request and since the
class would never reload on 2nd request, it would be silently reset
to an empty array. Same goes for any other method (my patch
actually dealt with the &lt;code&gt;id&lt;/code&gt; method also, but that was a
short-sighted fix since all methods are undef'ed).&lt;/p&gt;
&lt;p&gt;And I would add to your second suggestion:&lt;/p&gt;
&lt;p&gt;&lt;em&gt;If&lt;/em&gt; an explicit &lt;code&gt;require&lt;/code&gt; is needed, then
those model files should be loaded on app initialization and users
would need to understand they lose the reloading functionality for
those models in dev mode.&lt;/p&gt;
&lt;p&gt;As for you suggestion to persist objects to the Rails.cache, I
am not too familiar on how it works, but how would an object cope
with being tied to a Class that has been stripped from its method
declarations? Does the Class instance get cached too? And if so,
isn't it the same as not reloading it?&lt;/p&gt;&lt;/div&gt;</body-html>
      <closed type="boolean">false</closed>
      <created-at type="datetime">2009-01-08T16:55:22+00:00</created-at>
      <creator-id type="integer">28420</creator-id>
      <diffable-attributes type="yaml">--- {}

</diffable-attributes>
      <milestone-id type="integer">9903</milestone-id>
      <number type="integer">1339</number>
      <permalink>arbase-should-not-be-nuking-its-children-just-because-it-lost-interest</permalink>
      <priority type="integer">0</priority>
      <project-id type="integer">8994</project-id>
      <state>open</state>
      <tag>activerecord bug dependencies reload</tag>
      <title>AR::Base should not be nuking it's children, just because it lost interest</title>
      <updated-at type="datetime">2009-01-08T16:55:24+00:00</updated-at>
      <user-id type="integer">42669</user-id>
      <user-name>C. Bedard</user-name>
      <creator-name>Tom Lea</creator-name>
      <url>http://rails.lighthouseapp.com/projects/8994/tickets/1339</url>
      <milestone-title>2.x</milestone-title>
      <assigned-user-name>Jeremy Kemper</assigned-user-name>
    </version>
    <version type="Ticket::Version">
      <assigned-user-id type="integer">85</assigned-user-id>
      <attachments-count type="integer">3</attachments-count>
      <body>@Geoff: thanks for the explanation. I experiencing issues everytime that I set config.time_zone = 'Madrid' (I tried to set the time_zone = 'Paris' and I experiencing the same problem). If I comment this line and start the app with active_record.default_timezone :local or :utc, all works ok.</body>
      <body-html>&lt;div&gt;&lt;p&gt;@Geoff: thanks for the explanation. I experiencing issues
everytime that I set config.time_zone = 'Madrid' (I tried to set
the time_zone = 'Paris' and I experiencing the same problem). If I
comment this line and start the app with
active_record.default_timezone :local or :utc, all works ok.&lt;/p&gt;&lt;/div&gt;</body-html>
      <closed type="boolean">false</closed>
      <created-at type="datetime">2009-01-10T06:49:28+00:00</created-at>
      <creator-id type="integer">28420</creator-id>
      <diffable-attributes type="yaml">--- {}

</diffable-attributes>
      <milestone-id type="integer">9903</milestone-id>
      <number type="integer">1339</number>
      <permalink>arbase-should-not-be-nuking-its-children-just-because-it-lost-interest</permalink>
      <priority type="integer">0</priority>
      <project-id type="integer">8994</project-id>
      <state>open</state>
      <tag>activerecord bug dependencies reload</tag>
      <title>AR::Base should not be nuking it's children, just because it lost interest</title>
      <updated-at type="datetime">2009-01-10T06:49:35+00:00</updated-at>
      <user-id type="integer">33400</user-id>
      <user-name>Emili Parre&#241;o</user-name>
      <creator-name>Tom Lea</creator-name>
      <url>http://rails.lighthouseapp.com/projects/8994/tickets/1339</url>
      <milestone-title>2.x</milestone-title>
      <assigned-user-name>Jeremy Kemper</assigned-user-name>
    </version>
    <version type="Ticket::Version">
      <assigned-user-id type="integer">85</assigned-user-id>
      <attachments-count type="integer">3</attachments-count>
      <body>@C. Bedard -- if the Rails.cache memory store marshaled objects before storing, and unmarshaled them on retrieval, then it would never return instances of stripped-down classes: unmarshaling would re-create instances with current class definitions.

@Emili: to be clear, all works ok *if* you avoid calling methods that can't be generated via method_missing on cached instances.</body>
      <body-html>&lt;div&gt;&lt;p&gt;@C. Bedard -- if the Rails.cache memory store marshaled objects
before storing, and unmarshaled them on retrieval, then it would
never return instances of stripped-down classes: unmarshaling would
re-create instances with current class definitions.&lt;/p&gt;
&lt;p&gt;@Emili: to be clear, all works ok &lt;em&gt;if&lt;/em&gt; you avoid calling
methods that can't be generated via method_missing on cached
instances.&lt;/p&gt;&lt;/div&gt;</body-html>
      <closed type="boolean">false</closed>
      <created-at type="datetime">2009-01-11T20:53:59+00:00</created-at>
      <creator-id type="integer">28420</creator-id>
      <diffable-attributes type="yaml">--- {}

</diffable-attributes>
      <milestone-id type="integer">9903</milestone-id>
      <number type="integer">1339</number>
      <permalink>arbase-should-not-be-nuking-its-children-just-because-it-lost-interest</permalink>
      <priority type="integer">0</priority>
      <project-id type="integer">8994</project-id>
      <state>open</state>
      <tag>activerecord bug dependencies reload</tag>
      <title>AR::Base should not be nuking it's children, just because it lost interest</title>
      <updated-at type="datetime">2009-01-11T20:54:04+00:00</updated-at>
      <user-id type="integer">15783</user-id>
      <user-name>Geoff Buesing</user-name>
      <creator-name>Tom Lea</creator-name>
      <url>http://rails.lighthouseapp.com/projects/8994/tickets/1339</url>
      <milestone-title>2.x</milestone-title>
      <assigned-user-name>Jeremy Kemper</assigned-user-name>
    </version>
    <version type="Ticket::Version">
      <assigned-user-id type="integer">85</assigned-user-id>
      <attachments-count type="integer">3</attachments-count>
      <body>Perhaps the best bet here is to provide a patch which makes just the one potentially leaking change.  Ideally one that applies cleanly to 2.2

We can then ask people to run their apps in development with it  turned on and report the numbers back

It'd be great to fix this finally, and to do so knowing we're not going to be wasting too much ram</body>
      <body-html>&lt;div&gt;&lt;p&gt;Perhaps the best bet here is to provide a patch which makes just
the one potentially leaking change. Ideally one that applies
cleanly to 2.2&lt;/p&gt;
&lt;p&gt;We can then ask people to run their apps in development with it
turned on and report the numbers back&lt;/p&gt;
&lt;p&gt;It'd be great to fix this finally, and to do so knowing we're
not going to be wasting too much ram&lt;/p&gt;&lt;/div&gt;</body-html>
      <closed type="boolean">false</closed>
      <created-at type="datetime">2009-01-12T06:30:55+00:00</created-at>
      <creator-id type="integer">28420</creator-id>
      <diffable-attributes type="yaml">--- {}

</diffable-attributes>
      <milestone-id type="integer">9903</milestone-id>
      <number type="integer">1339</number>
      <permalink>arbase-should-not-be-nuking-its-children-just-because-it-lost-interest</permalink>
      <priority type="integer">0</priority>
      <project-id type="integer">8994</project-id>
      <state>open</state>
      <tag>activerecord bug dependencies reload</tag>
      <title>AR::Base should not be nuking it's children, just because it lost interest</title>
      <updated-at type="datetime">2009-01-12T06:30:59+00:00</updated-at>
      <user-id type="integer">141</user-id>
      <user-name>Michael Koziarski</user-name>
      <creator-name>Tom Lea</creator-name>
      <url>http://rails.lighthouseapp.com/projects/8994/tickets/1339</url>
      <milestone-title>2.x</milestone-title>
      <assigned-user-name>Jeremy Kemper</assigned-user-name>
    </version>
    <version type="Ticket::Version">
      <assigned-user-id type="integer">85</assigned-user-id>
      <attachments-count type="integer">3</attachments-count>
      <body>@Koz would that be a patch along the lines of the original one attached to the ticket? or something else? I'm happy to rebase it if that is the case. It is definitely leaky in dev, but just how leaky in a large app is still unknown.</body>
      <body-html>&lt;div&gt;&lt;p&gt;@Koz would that be a patch along the lines of the original one
attached to the ticket? or something else? I'm happy to rebase it
if that is the case. It is definitely leaky in dev, but just how
leaky in a large app is still unknown.&lt;/p&gt;&lt;/div&gt;</body-html>
      <closed type="boolean">false</closed>
      <created-at type="datetime">2009-01-12T08:46:52+00:00</created-at>
      <creator-id type="integer">28420</creator-id>
      <diffable-attributes type="yaml">--- {}

</diffable-attributes>
      <milestone-id type="integer">9903</milestone-id>
      <number type="integer">1339</number>
      <permalink>arbase-should-not-be-nuking-its-children-just-because-it-lost-interest</permalink>
      <priority type="integer">0</priority>
      <project-id type="integer">8994</project-id>
      <state>open</state>
      <tag>activerecord bug dependencies reload</tag>
      <title>AR::Base should not be nuking it's children, just because it lost interest</title>
      <updated-at type="datetime">2009-01-12T08:46:54+00:00</updated-at>
      <user-id type="integer">28420</user-id>
      <user-name>Tom Lea</user-name>
      <creator-name>Tom Lea</creator-name>
      <url>http://rails.lighthouseapp.com/projects/8994/tickets/1339</url>
      <milestone-title>2.x</milestone-title>
      <assigned-user-name>Jeremy Kemper</assigned-user-name>
    </version>
    <version type="Ticket::Version">
      <assigned-user-id type="integer">85</assigned-user-id>
      <attachments-count type="integer">3</attachments-count>
      <body>Yep, if it still applies cleanly to master and/or 2-2-stable just let me know.</body>
      <body-html>&lt;div&gt;&lt;p&gt;Yep, if it still applies cleanly to master and/or 2-2-stable
just let me know.&lt;/p&gt;&lt;/div&gt;</body-html>
      <closed type="boolean">false</closed>
      <created-at type="datetime">2009-01-12T09:10:06+00:00</created-at>
      <creator-id type="integer">28420</creator-id>
      <diffable-attributes type="yaml">--- {}

</diffable-attributes>
      <milestone-id type="integer">9903</milestone-id>
      <number type="integer">1339</number>
      <permalink>arbase-should-not-be-nuking-its-children-just-because-it-lost-interest</permalink>
      <priority type="integer">0</priority>
      <project-id type="integer">8994</project-id>
      <state>open</state>
      <tag>activerecord bug dependencies reload</tag>
      <title>AR::Base should not be nuking it's children, just because it lost interest</title>
      <updated-at type="datetime">2009-01-12T09:10:08+00:00</updated-at>
      <user-id type="integer">141</user-id>
      <user-name>Michael Koziarski</user-name>
      <creator-name>Tom Lea</creator-name>
      <url>http://rails.lighthouseapp.com/projects/8994/tickets/1339</url>
      <milestone-title>2.x</milestone-title>
      <assigned-user-name>Jeremy Kemper</assigned-user-name>
    </version>
    <version type="Ticket::Version">
      <assigned-user-id type="integer">85</assigned-user-id>
      <attachments-count type="integer">3</attachments-count>
      <body>The attached patch applies cleanly to both master and 2-2-stable, I've updated the commit note to be a bit more descriptive, but obviously feel free to edit.</body>
      <body-html>&lt;div&gt;&lt;p&gt;The attached patch applies cleanly to both master and
2-2-stable, I've updated the commit note to be a bit more
descriptive, but obviously feel free to edit.&lt;/p&gt;&lt;/div&gt;</body-html>
      <closed type="boolean">false</closed>
      <created-at type="datetime">2009-01-12T10:28:26+00:00</created-at>
      <creator-id type="integer">28420</creator-id>
      <diffable-attributes type="yaml">--- {}

</diffable-attributes>
      <milestone-id type="integer">9903</milestone-id>
      <number type="integer">1339</number>
      <permalink>arbase-should-not-be-nuking-its-children-just-because-it-lost-interest</permalink>
      <priority type="integer">0</priority>
      <project-id type="integer">8994</project-id>
      <state>open</state>
      <tag>activerecord bug dependencies reload</tag>
      <title>AR::Base should not be nuking it's children, just because it lost interest</title>
      <updated-at type="datetime">2009-01-12T10:28:30+00:00</updated-at>
      <user-id type="integer">28420</user-id>
      <user-name>Tom Lea</user-name>
      <creator-name>Tom Lea</creator-name>
      <url>http://rails.lighthouseapp.com/projects/8994/tickets/1339</url>
      <milestone-title>2.x</milestone-title>
      <assigned-user-name>Jeremy Kemper</assigned-user-name>
    </version>
    <version type="Ticket::Version">
      <assigned-user-id type="integer">85</assigned-user-id>
      <attachments-count type="integer">4</attachments-count>
      <body>@Geoff: You're saying &quot;...then it would never return instances of stripped-down classes: unmarshaling would re-create instances with current class definitions.&quot; 

From what I understand, the &quot;current class definitions&quot; after reloading *are* stripped-down. So if an explicit `require` (as in my example app) was used (mocking up the Dependencies autoload), marshalling an instance would not change the fact that its Class has lost its methods since it is not reloaded.

@Michael: I have tested the patch and as I mentioned earlier, it does leak in dev mode. Here are my test results (same app, same request 100 times in a loop):

With patch:
   Initial memory usage:  40 092 Kb
   after 100 requests  : 120 104 Kb

Without patch:
   Initial memory usage:  40 168 Kb
   after 100 requests  :  61 824 Kb

The leak seems to be proportional to the number of methods and attributes of all AR subclasses (which makes sense since it is the instance_vars and instance_methods that do not get cleared anymore). 

Note that memory usage keeps growing in dev mode regardless of the patch (probably pointing to other leaks in dev mode (?).</body>
      <body-html>&lt;div&gt;&lt;p&gt;@Geoff: You're saying &quot;...then it would never return instances
of stripped-down classes: unmarshaling would re-create instances
with current class definitions.&quot;&lt;/p&gt;
&lt;p&gt;From what I understand, the &quot;current class definitions&quot; after
reloading &lt;em&gt;are&lt;/em&gt; stripped-down. So if an explicit
&lt;code&gt;require&lt;/code&gt; (as in my example app) was used (mocking up
the Dependencies autoload), marshalling an instance would not
change the fact that its Class has lost its methods since it is not
reloaded.&lt;/p&gt;
&lt;p&gt;@Michael: I have tested the patch and as I mentioned earlier, it
does leak in dev mode. Here are my test results (same app, same
request 100 times in a loop):&lt;/p&gt;
&lt;p&gt;With patch: Initial memory usage: 40 092 Kb after 100 requests :
120 104 Kb&lt;/p&gt;
&lt;p&gt;Without patch: Initial memory usage: 40 168 Kb after 100
requests : 61 824 Kb&lt;/p&gt;
&lt;p&gt;The leak seems to be proportional to the number of methods and
attributes of all AR subclasses (which makes sense since it is the
instance_vars and instance_methods that do not get cleared
anymore).&lt;/p&gt;
&lt;p&gt;Note that memory usage keeps growing in dev mode regardless of
the patch (probably pointing to other leaks in dev mode (?).&lt;/p&gt;&lt;/div&gt;</body-html>
      <closed type="boolean">false</closed>
      <created-at type="datetime">2009-01-12T17:27:54+00:00</created-at>
      <creator-id type="integer">28420</creator-id>
      <diffable-attributes type="yaml">--- {}

</diffable-attributes>
      <milestone-id type="integer">9903</milestone-id>
      <number type="integer">1339</number>
      <permalink>arbase-should-not-be-nuking-its-children-just-because-it-lost-interest</permalink>
      <priority type="integer">0</priority>
      <project-id type="integer">8994</project-id>
      <state>open</state>
      <tag>activerecord bug dependencies reload</tag>
      <title>AR::Base should not be nuking it's children, just because it lost interest</title>
      <updated-at type="datetime">2009-01-12T17:28:00+00:00</updated-at>
      <user-id type="integer">42669</user-id>
      <user-name>C. Bedard</user-name>
      <creator-name>Tom Lea</creator-name>
      <url>http://rails.lighthouseapp.com/projects/8994/tickets/1339</url>
      <milestone-title>2.x</milestone-title>
      <assigned-user-name>Jeremy Kemper</assigned-user-name>
    </version>
    <version type="Ticket::Version">
      <assigned-user-id type="integer">85</assigned-user-id>
      <attachments-count type="integer">4</attachments-count>
      <body>Also, come to think of it: Tom's patch, by not stripping down the subclasses, does not address the underlying issue (which is really that an AR subclass might not be reloaded on each request because Dependencies has skipped it if it was explicitly required somewhere). So basically, the error is not raised anymore because the app holds on to the initial class definition which is not being stripped-down anymore. 

As my patch did, it only hides the fact that the Class is not being reloaded.

You can try this with the Monkey App with the patch applied: Launch the app, hit it with a first request, then remove the `def &gt;(object)` method. If you hit the server again with more  requests, no error is raised, and the `&gt;` method is still defined (which it should not if the class has been reloaded).
`MONKEY_ONE.class == @monkey_two.class # =&gt; false`</body>
      <body-html>&lt;div&gt;&lt;p&gt;Also, come to think of it: Tom's patch, by not stripping down
the subclasses, does not address the underlying issue (which is
really that an AR subclass might not be reloaded on each request
because Dependencies has skipped it if it was explicitly required
somewhere). So basically, the error is not raised anymore because
the app holds on to the initial class definition which is not being
stripped-down anymore.&lt;/p&gt;
&lt;p&gt;As my patch did, it only hides the fact that the Class is not
being reloaded.&lt;/p&gt;
&lt;p&gt;You can try this with the Monkey App with the patch applied:
Launch the app, hit it with a first request, then remove the
&lt;code&gt;def &amp;gt;(object)&lt;/code&gt; method. If you hit the server again
with more requests, no error is raised, and the &lt;code&gt;&amp;gt;&lt;/code&gt;
method is still defined (which it should not if the class has been
reloaded). &lt;code&gt;MONKEY_ONE.class == @monkey_two.class # =&amp;gt;
false&lt;/code&gt;&lt;/p&gt;&lt;/div&gt;</body-html>
      <closed type="boolean">false</closed>
      <created-at type="datetime">2009-01-12T18:11:41+00:00</created-at>
      <creator-id type="integer">28420</creator-id>
      <diffable-attributes type="yaml">--- {}

</diffable-attributes>
      <milestone-id type="integer">9903</milestone-id>
      <number type="integer">1339</number>
      <permalink>arbase-should-not-be-nuking-its-children-just-because-it-lost-interest</permalink>
      <priority type="integer">0</priority>
      <project-id type="integer">8994</project-id>
      <state>open</state>
      <tag>activerecord bug dependencies reload</tag>
      <title>AR::Base should not be nuking it's children, just because it lost interest</title>
      <updated-at type="datetime">2009-01-12T18:11:46+00:00</updated-at>
      <user-id type="integer">42669</user-id>
      <user-name>C. Bedard</user-name>
      <creator-name>Tom Lea</creator-name>
      <url>http://rails.lighthouseapp.com/projects/8994/tickets/1339</url>
      <milestone-title>2.x</milestone-title>
      <assigned-user-name>Jeremy Kemper</assigned-user-name>
    </version>
    <version type="Ticket::Version">
      <assigned-user-id type="integer">85</assigned-user-id>
      <attachments-count type="integer">4</attachments-count>
      <body>@C. Bedard: Just to be clear, the patch does not make magic happen, all instances remain instances of the class they always were instances off. When the contsants that reference the classes (the constant `Monkey` in this case) are undefined by Dependencies, the instances still refer to the classes they were instantiated from.

This is how it works with standard classes (Object.remove_const is effectively the reload):

@@@ ruby
class Foo
  def bar
    10
  end
end

baz = Foo.new
baz.bar #=&gt; 10

Object.send :remove_const, :Foo

class Foo
  def bar
    20
  end
end

baz.bar #=&gt; 10

bam = Foo.new
bam.bar #=&gt; 20
@@@

`baz` is and always will be an instance of the first Foo (from before the reload), and this is always the case. It cannot be changed and will refuse to mutate.

The same is true of active record classes. The only difference here is that special code (that code removed by the patch) strips out the original instance methods, and leaves the instances disfigured and unpleasant to the touch.

The class has been redefined, and new instances will behave as expected, but existing instances will be different.

Another example to further the point that Monkey and Monkey after the reload are different entities:

@@@ ruby
class Foo
end

foo_one = Foo.object_id

Object.send :remove_const, :Foo

class Foo
end

foo_two = Foo.object_id

p :foo_one =&gt; foo_one, :foo_two =&gt; foo_two

# &gt;&gt; {:foo_one=&gt;1701840, :foo_two=&gt;1701810}
@@@

So in conclusion, classes are not reloaded, they are recreated. Making them reload is for another time and another place... and is not easy looking.

p.s. If I lost my point somewhere in there, it's because it's 6:42pm here, and I've had a long day of I18n cards ;).</body>
      <body-html>&lt;div&gt;&lt;p&gt;@C. Bedard: Just to be clear, the patch does not make magic
happen, all instances remain instances of the class they always
were instances off. When the contsants that reference the classes
(the constant &lt;code&gt;Monkey&lt;/code&gt; in this case) are undefined by
Dependencies, the instances still refer to the classes they were
instantiated from.&lt;/p&gt;
&lt;p&gt;This is how it works with standard classes (Object.remove_const
is effectively the reload):&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;
class Foo
  def bar
    10
  end
end

baz = Foo.new
baz.bar #=&amp;gt; 10

Object.send :remove_const, :Foo

class Foo
  def bar
    20
  end
end

baz.bar #=&amp;gt; 10

bam = Foo.new
bam.bar #=&amp;gt; 20
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code&gt;baz&lt;/code&gt; is and always will be an instance of the first
Foo (from before the reload), and this is always the case. It
cannot be changed and will refuse to mutate.&lt;/p&gt;
&lt;p&gt;The same is true of active record classes. The only difference
here is that special code (that code removed by the patch) strips
out the original instance methods, and leaves the instances
disfigured and unpleasant to the touch.&lt;/p&gt;
&lt;p&gt;The class has been redefined, and new instances will behave as
expected, but existing instances will be different.&lt;/p&gt;
&lt;p&gt;Another example to further the point that Monkey and Monkey
after the reload are different entities:&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;
class Foo
end

foo_one = Foo.object_id

Object.send :remove_const, :Foo

class Foo
end

foo_two = Foo.object_id

p :foo_one =&amp;gt; foo_one, :foo_two =&amp;gt; foo_two

# &amp;gt;&amp;gt; {:foo_one=&amp;gt;1701840, :foo_two=&amp;gt;1701810}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;So in conclusion, classes are not reloaded, they are recreated.
Making them reload is for another time and another place... and is
not easy looking.&lt;/p&gt;
&lt;p&gt;p.s. If I lost my point somewhere in there, it's because it's
6:42pm here, and I've had a long day of I18n cards ;).&lt;/p&gt;&lt;/div&gt;</body-html>
      <closed type="boolean">false</closed>
      <created-at type="datetime">2009-01-12T18:43:21+00:00</created-at>
      <creator-id type="integer">28420</creator-id>
      <diffable-attributes type="yaml">--- {}

</diffable-attributes>
      <milestone-id type="integer">9903</milestone-id>
      <number type="integer">1339</number>
      <permalink>arbase-should-not-be-nuking-its-children-just-because-it-lost-interest</permalink>
      <priority type="integer">0</priority>
      <project-id type="integer">8994</project-id>
      <state>open</state>
      <tag>activerecord bug dependencies reload</tag>
      <title>AR::Base should not be nuking it's children, just because it lost interest</title>
      <updated-at type="datetime">2009-01-12T18:43:27+00:00</updated-at>
      <user-id type="integer">28420</user-id>
      <user-name>Tom Lea</user-name>
      <creator-name>Tom Lea</creator-name>
      <url>http://rails.lighthouseapp.com/projects/8994/tickets/1339</url>
      <milestone-title>2.x</milestone-title>
      <assigned-user-name>Jeremy Kemper</assigned-user-name>
    </version>
    <version type="Ticket::Version">
      <assigned-user-id type="integer">85</assigned-user-id>
      <attachments-count type="integer">4</attachments-count>
      <body>This caused me a major headache. I am modifying Community Engine and was getting first the time zone error, then I set environment to use local time and I got random NoMethod on second page load on methods defined in my models. I tried the patch and it fixes my issue.</body>
      <body-html>&lt;div&gt;&lt;p&gt;This caused me a major headache. I am modifying Community Engine
and was getting first the time zone error, then I set environment
to use local time and I got random NoMethod on second page load on
methods defined in my models. I tried the patch and it fixes my
issue.&lt;/p&gt;&lt;/div&gt;</body-html>
      <closed type="boolean">false</closed>
      <created-at type="datetime">2009-01-25T07:08:53+00:00</created-at>
      <creator-id type="integer">28420</creator-id>
      <diffable-attributes type="yaml">--- {}

</diffable-attributes>
      <milestone-id type="integer">9903</milestone-id>
      <number type="integer">1339</number>
      <permalink>arbase-should-not-be-nuking-its-children-just-because-it-lost-interest</permalink>
      <priority type="integer">0</priority>
      <project-id type="integer">8994</project-id>
      <state>open</state>
      <tag>activerecord bug dependencies reload</tag>
      <title>AR::Base should not be nuking it's children, just because it lost interest</title>
      <updated-at type="datetime">2009-01-25T07:08:56+00:00</updated-at>
      <user-id type="integer">44881</user-id>
      <user-name>Christian Gregertsen</user-name>
      <creator-name>Tom Lea</creator-name>
      <url>http://rails.lighthouseapp.com/projects/8994/tickets/1339</url>
      <milestone-title>2.x</milestone-title>
      <assigned-user-name>Jeremy Kemper</assigned-user-name>
    </version>
    <version type="Ticket::Version">
      <assigned-user-id type="integer">85</assigned-user-id>
      <attachments-count type="integer">4</attachments-count>
      <body>I see similar leak statistics to What C. Bedard mentioned.  60M after 100 requests.

This isn't an acceptable cost to pay for this problem sorry, it's around 600k per request for an app with only around 15 models and associated associations.  So we need another fix which doesn't leak that memory.


Another option would be to make the 'vestigial classes' have a method_missing which showed a very detailed error message about what happened, and what they can do about it.</body>
      <body-html>&lt;div&gt;&lt;p&gt;I see similar leak statistics to What C. Bedard mentioned. 60M
after 100 requests.&lt;/p&gt;
&lt;p&gt;This isn't an acceptable cost to pay for this problem sorry,
it's around 600k per request for an app with only around 15 models
and associated associations. So we need another fix which doesn't
leak that memory.&lt;/p&gt;
&lt;p&gt;Another option would be to make the 'vestigial classes' have a
method_missing which showed a very detailed error message about
what happened, and what they can do about it.&lt;/p&gt;&lt;/div&gt;</body-html>
      <closed type="boolean">false</closed>
      <created-at type="datetime">2009-01-26T03:19:21+00:00</created-at>
      <creator-id type="integer">28420</creator-id>
      <diffable-attributes type="yaml">--- {}

</diffable-attributes>
      <milestone-id type="integer">9903</milestone-id>
      <number type="integer">1339</number>
      <permalink>arbase-should-not-be-nuking-its-children-just-because-it-lost-interest</permalink>
      <priority type="integer">0</priority>
      <project-id type="integer">8994</project-id>
      <state>open</state>
      <tag>activerecord bug dependencies reload</tag>
      <title>AR::Base should not be nuking it's children, just because it lost interest</title>
      <updated-at type="datetime">2009-01-26T03:19:23+00:00</updated-at>
      <user-id type="integer">141</user-id>
      <user-name>Michael Koziarski</user-name>
      <creator-name>Tom Lea</creator-name>
      <url>http://rails.lighthouseapp.com/projects/8994/tickets/1339</url>
      <milestone-title>2.x</milestone-title>
      <assigned-user-name>Jeremy Kemper</assigned-user-name>
    </version>
    <version type="Ticket::Version">
      <assigned-user-id type="integer">85</assigned-user-id>
      <attachments-count type="integer">4</attachments-count>
      <body>Even after applying the patch I get an error at the 1st reload of a page, when I include a related model in the caching:

&lt;code&gt;class Project &lt; ActiveRecord::Base
...
belongs_to :person
...
def self.all_cached
  Rails.cache.fetch('Project.all_sort_name') { Project.sname_sort }
end

def self.sname_sort
  Project.all(:include =&gt; 'person', :order =&gt; 'people.sname ASC')
end&lt;/code&gt;

This gives me a

&lt;code&gt;Argument error &quot;undefined class/module Person&quot;&lt;/code&gt; 

after reloading (2nd display) in development mode. This doesn't happen in production mode, or after terminating/restarting memcached which is used as caching server.</body>
      <body-html>&lt;div&gt;&lt;p&gt;Even after applying the patch I get an error at the 1st reload
of a page, when I include a related model in the caching:&lt;/p&gt;
&lt;p&gt;&lt;code&gt;class Project &amp;lt; ActiveRecord::Base ... belongs_to
:person ... def self.all_cached
Rails.cache.fetch('Project.all_sort_name') { Project.sname_sort }
end&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;def self.sname_sort Project.all(:include =&amp;gt; 'person', :order
=&amp;gt; 'people.sname ASC') end&lt;/p&gt;
&lt;p&gt;This gives me a&lt;/p&gt;
&lt;p&gt;&lt;code&gt;Argument error &quot;undefined class/module Person&quot;&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;after reloading (2nd display) in development mode. This doesn't
happen in production mode, or after terminating/restarting
memcached which is used as caching server.&lt;/p&gt;&lt;/div&gt;</body-html>
      <closed type="boolean">false</closed>
      <created-at type="datetime">2009-02-09T14:38:40+00:00</created-at>
      <creator-id type="integer">28420</creator-id>
      <diffable-attributes type="yaml">--- {}

</diffable-attributes>
      <milestone-id type="integer">9903</milestone-id>
      <number type="integer">1339</number>
      <permalink>arbase-should-not-be-nuking-its-children-just-because-it-lost-interest</permalink>
      <priority type="integer">0</priority>
      <project-id type="integer">8994</project-id>
      <state>open</state>
      <tag>activerecord bug dependencies reload</tag>
      <title>AR::Base should not be nuking it's children, just because it lost interest</title>
      <updated-at type="datetime">2009-02-09T14:38:42+00:00</updated-at>
      <user-id type="integer">46732</user-id>
      <user-name>Frank W&#246;ckener</user-name>
      <creator-name>Tom Lea</creator-name>
      <url>http://rails.lighthouseapp.com/projects/8994/tickets/1339</url>
      <milestone-title>2.x</milestone-title>
      <assigned-user-name>Jeremy Kemper</assigned-user-name>
    </version>
    <version type="Ticket::Version">
      <assigned-user-id type="integer">85</assigned-user-id>
      <attachments-count type="integer">4</attachments-count>
      <body>What's the status of this bug ? I have this problem in JRuby environment.

Can we just add :default param for default value of class_inheritable_accessor ? 

example:

@@@ ruby
base.class_inheritable_accessor :skip_time_zone_conversion_for_attributes, :instance_writer =&gt; false, :default =&gt; []
@@@</body>
      <body-html>&lt;div&gt;&lt;p&gt;What's the status of this bug ? I have this problem in JRuby
environment.&lt;/p&gt;
&lt;p&gt;Can we just add :default param for default value of
class_inheritable_accessor ?&lt;/p&gt;
&lt;p&gt;example:&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;
base.class_inheritable_accessor :skip_time_zone_conversion_for_attributes, :instance_writer =&amp;gt; false, :default =&amp;gt; []
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;</body-html>
      <closed type="boolean">false</closed>
      <created-at type="datetime">2009-03-06T19:05:33+00:00</created-at>
      <creator-id type="integer">28420</creator-id>
      <diffable-attributes type="yaml">--- {}

</diffable-attributes>
      <milestone-id type="integer">9903</milestone-id>
      <number type="integer">1339</number>
      <permalink>arbase-should-not-be-nuking-its-children-just-because-it-lost-interest</permalink>
      <priority type="integer">0</priority>
      <project-id type="integer">8994</project-id>
      <state>open</state>
      <tag>activerecord bug dependencies reload</tag>
      <title>AR::Base should not be nuking it's children, just because it lost interest</title>
      <updated-at type="datetime">2009-03-06T19:05:37+00:00</updated-at>
      <user-id type="integer">11785</user-id>
      <user-name>Pawe&#322; Kondzior</user-name>
      <creator-name>Tom Lea</creator-name>
      <url>http://rails.lighthouseapp.com/projects/8994/tickets/1339</url>
      <milestone-title>2.x</milestone-title>
      <assigned-user-name>Jeremy Kemper</assigned-user-name>
    </version>
    <version type="Ticket::Version">
      <assigned-user-id type="integer">85</assigned-user-id>
      <attachments-count type="integer">4</attachments-count>
      <body>@pawel: As far as I am aware, this is still unresolved.

The 'real world' memory leaks reported are unacceptable. Much bigger than I expected, so this solution is officially pants.

Sorry I've gone a little 'dark' on this one, as I am basically out of ideas, and am no longer feeling the hurt at work. The 'vestigial classes' solution is not one I am a particularly large fan of, but it may well be the only suitable compromise for the general case (not a cache store).

We could still do well to patch the MemoryStore to do one of the following:

* Marshal everything (just like the file store), but this takes an unknown performance hit.
* Marshal when not caching classes, to allow testing caching stuff in dev mode.
* Make MemoryStore a null cache when reloading classes is enabled.
* Refuse to use MemoryStore when class reloading is enabled.

The first one may compromise the performance of the memory store, the second two feel like nasty nasty hacks, and I'm still not sure about the last one, feels too violent.

Anyone fancy writing a patch for the vestigial classes to see how it feels?
Anyone got any more thoughts on dealing with the memory cache?</body>
      <body-html>&lt;div&gt;&lt;p&gt;@pawel: As far as I am aware, this is still unresolved.&lt;/p&gt;
&lt;p&gt;The 'real world' memory leaks reported are unacceptable. Much
bigger than I expected, so this solution is officially pants.&lt;/p&gt;
&lt;p&gt;Sorry I've gone a little 'dark' on this one, as I am basically
out of ideas, and am no longer feeling the hurt at work. The
'vestigial classes' solution is not one I am a particularly large
fan of, but it may well be the only suitable compromise for the
general case (not a cache store).&lt;/p&gt;
&lt;p&gt;We could still do well to patch the MemoryStore to do one of the
following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Marshal everything (just like the file store), but this takes
an unknown performance hit.&lt;/li&gt;
&lt;li&gt;Marshal when not caching classes, to allow testing caching
stuff in dev mode.&lt;/li&gt;
&lt;li&gt;Make MemoryStore a null cache when reloading classes is
enabled.&lt;/li&gt;
&lt;li&gt;Refuse to use MemoryStore when class reloading is enabled.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The first one may compromise the performance of the memory
store, the second two feel like nasty nasty hacks, and I'm still
not sure about the last one, feels too violent.&lt;/p&gt;
&lt;p&gt;Anyone fancy writing a patch for the vestigial classes to see
how it feels? Anyone got any more thoughts on dealing with the
memory cache?&lt;/p&gt;&lt;/div&gt;</body-html>
      <closed type="boolean">false</closed>
      <created-at type="datetime">2009-03-06T22:21:56+00:00</created-at>
      <creator-id type="integer">28420</creator-id>
      <diffable-attributes type="yaml">--- {}

</diffable-attributes>
      <milestone-id type="integer">9903</milestone-id>
      <number type="integer">1339</number>
      <permalink>arbase-should-not-be-nuking-its-children-just-because-it-lost-interest</permalink>
      <priority type="integer">0</priority>
      <project-id type="integer">8994</project-id>
      <state>open</state>
      <tag>activerecord bug dependencies reload</tag>
      <title>AR::Base should not be nuking it's children, just because it lost interest</title>
      <updated-at type="datetime">2009-03-06T22:22:03+00:00</updated-at>
      <user-id type="integer">28420</user-id>
      <user-name>Tom Lea</user-name>
      <creator-name>Tom Lea</creator-name>
      <url>http://rails.lighthouseapp.com/projects/8994/tickets/1339</url>
      <milestone-title>2.x</milestone-title>
      <assigned-user-name>Jeremy Kemper</assigned-user-name>
    </version>
    <version type="Ticket::Version">
      <assigned-user-id type="integer">85</assigned-user-id>
      <attachments-count type="integer">4</attachments-count>
      <body>The issue is slightly broader than just the cache stores though,
you'll have the same issues if you used:

$foo ||= Foo.first

# next request

$foo.bar!

Perhaps we could raise a particular exception, and document that
exception to describe the different issues that can cause it?</body>
      <body-html>&lt;div&gt;&lt;p&gt;The issue is slightly broader than just the cache stores though,
you'll have the same issues if you used:&lt;/p&gt;
&lt;p&gt;$foo ||= Foo.first&lt;/p&gt;
&lt;h1&gt;next request&lt;/h1&gt;
&lt;p&gt;$foo.bar!&lt;/p&gt;
&lt;p&gt;Perhaps we could raise a particular exception, and document that
exception to describe the different issues that can cause it?&lt;/p&gt;&lt;/div&gt;</body-html>
      <closed type="boolean">false</closed>
      <created-at type="datetime">2009-03-09T04:23:07+00:00</created-at>
      <creator-id type="integer">28420</creator-id>
      <diffable-attributes type="yaml">--- {}

</diffable-attributes>
      <milestone-id type="integer">9903</milestone-id>
      <number type="integer">1339</number>
      <permalink>arbase-should-not-be-nuking-its-children-just-because-it-lost-interest</permalink>
      <priority type="integer">0</priority>
      <project-id type="integer">8994</project-id>
      <state>open</state>
      <tag>activerecord bug dependencies reload</tag>
      <title>AR::Base should not be nuking it's children, just because it lost interest</title>
      <updated-at type="datetime">2009-03-09T04:23:11+00:00</updated-at>
      <user-id type="integer">141</user-id>
      <user-name>Michael Koziarski</user-name>
      <creator-name>Tom Lea</creator-name>
      <url>http://rails.lighthouseapp.com/projects/8994/tickets/1339</url>
      <milestone-title>2.x</milestone-title>
      <assigned-user-name>Jeremy Kemper</assigned-user-name>
    </version>
    <version type="Ticket::Version">
      <assigned-user-id type="integer">85</assigned-user-id>
      <attachments-count type="integer">4</attachments-count>
      <body>Sure.  That's the issue with using global vars, and class vars have exactly the same issue.

I don't see what the big deal is here.  Why aren't we just all using class level instance vars which only get initialized when they need to, like the following pastie:

http://gist.github.com/76130

This makes the thing lazy, and if the class magically goes away (via reloading through Module#remove_const), the data just gets reloaded the next time it is accessed.

Am I missing something here?

On another note, I like the exception idea, but I don't think there is any way you are going to be able to track absolutely everything that an object maybe holding on to, with the excepting of going to mod_ruby or invoking GC#each_object.</body>
      <body-html>&lt;div&gt;&lt;p&gt;Sure. That's the issue with using global vars, and class vars
have exactly the same issue.&lt;/p&gt;
&lt;p&gt;I don't see what the big deal is here. Why aren't we just all
using class level instance vars which only get initialized when
they need to, like the following pastie:&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;http://gist.github.com/76130&quot;&gt;http://gist.github.com/76130&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;This makes the thing lazy, and if the class magically goes away
(via reloading through Module#remove_const), the data just gets
reloaded the next time it is accessed.&lt;/p&gt;
&lt;p&gt;Am I missing something here?&lt;/p&gt;
&lt;p&gt;On another note, I like the exception idea, but I don't think
there is any way you are going to be able to track absolutely
everything that an object maybe holding on to, with the excepting
of going to mod_ruby or invoking GC#each_object.&lt;/p&gt;&lt;/div&gt;</body-html>
      <closed type="boolean">false</closed>
      <created-at type="datetime">2009-03-09T06:13:18+00:00</created-at>
      <creator-id type="integer">28420</creator-id>
      <diffable-attributes type="yaml">--- {}

</diffable-attributes>
      <milestone-id type="integer">9903</milestone-id>
      <number type="integer">1339</number>
      <permalink>arbase-should-not-be-nuking-its-children-just-because-it-lost-interest</permalink>
      <priority type="integer">0</priority>
      <project-id type="integer">8994</project-id>
      <state>open</state>
      <tag>activerecord bug dependencies reload</tag>
      <title>AR::Base should not be nuking it's children, just because it lost interest</title>
      <updated-at type="datetime">2009-03-09T06:13:23+00:00</updated-at>
      <user-id type="integer">10212</user-id>
      <user-name>Scott Taylor</user-name>
      <creator-name>Tom Lea</creator-name>
      <url>http://rails.lighthouseapp.com/projects/8994/tickets/1339</url>
      <milestone-title>2.x</milestone-title>
      <assigned-user-name>Jeremy Kemper</assigned-user-name>
    </version>
    <version type="Ticket::Version">
      <assigned-user-id type="integer">85</assigned-user-id>
      <attachments-count type="integer">4</attachments-count>
      <body>Class variables behave fine (with respect to this bug) if they're
class vars on active record subclasses.  e.g.

class User &lt; AR::Base
  cattr_accessor :most_popular
end

That's why making MemoryStore marshall the objects would be a
reasonable fix as beyond caching there's not many cases which can't be
more easily solved by slightly changing the design.

As for the exception, I was simply thinking about overriding
method_missing on the AR::Base subclass when we're calling
undef_method to just raise the exception immediately, irrespective of
the method or arguments.</body>
      <body-html>&lt;div&gt;&lt;p&gt;Class variables behave fine (with respect to this bug) if
they're class vars on active record subclasses. e.g.&lt;/p&gt;
&lt;p&gt;class User &amp;lt; AR::Base cattr_accessor :most_popular end&lt;/p&gt;
&lt;p&gt;That's why making MemoryStore marshall the objects would be a
reasonable fix as beyond caching there's not many cases which can't
be more easily solved by slightly changing the design.&lt;/p&gt;
&lt;p&gt;As for the exception, I was simply thinking about overriding
method_missing on the AR::Base subclass when we're calling
undef_method to just raise the exception immediately, irrespective
of the method or arguments.&lt;/p&gt;&lt;/div&gt;</body-html>
      <closed type="boolean">false</closed>
      <created-at type="datetime">2009-03-09T07:13:36+00:00</created-at>
      <creator-id type="integer">28420</creator-id>
      <diffable-attributes type="yaml">--- {}

</diffable-attributes>
      <milestone-id type="integer">9903</milestone-id>
      <number type="integer">1339</number>
      <permalink>arbase-should-not-be-nuking-its-children-just-because-it-lost-interest</permalink>
      <priority type="integer">0</priority>
      <project-id type="integer">8994</project-id>
      <state>open</state>
      <tag>activerecord bug dependencies reload</tag>
      <title>AR::Base should not be nuking it's children, just because it lost interest</title>
      <updated-at type="datetime">2009-03-09T07:13:39+00:00</updated-at>
      <user-id type="integer">141</user-id>
      <user-name>Michael Koziarski</user-name>
      <creator-name>Tom Lea</creator-name>
      <url>http://rails.lighthouseapp.com/projects/8994/tickets/1339</url>
      <milestone-title>2.x</milestone-title>
      <assigned-user-name>Jeremy Kemper</assigned-user-name>
    </version>
    <version type="Ticket::Version">
      <assigned-user-id type="integer">85</assigned-user-id>
      <attachments-count type="integer">4</attachments-count>
      <body>So what has become the conventional way of dealing with this issue? To use Marshall? If so, does that imply having to create de/serializers too?

Apologies, I'm new (but have found myself hitting this problem)!</body>
      <body-html>&lt;div&gt;&lt;p&gt;So what has become the conventional way of dealing with this
issue? To use Marshall? If so, does that imply having to create
de/serializers too?&lt;/p&gt;
&lt;p&gt;Apologies, I'm new (but have found myself hitting this
problem)!&lt;/p&gt;&lt;/div&gt;</body-html>
      <closed type="boolean">false</closed>
      <created-at type="datetime">2009-04-06T16:22:20+01:00</created-at>
      <creator-id type="integer">28420</creator-id>
      <diffable-attributes type="yaml">--- {}

</diffable-attributes>
      <milestone-id type="integer">9903</milestone-id>
      <number type="integer">1339</number>
      <permalink>arbase-should-not-be-nuking-its-children-just-because-it-lost-interest</permalink>
      <priority type="integer">0</priority>
      <project-id type="integer">8994</project-id>
      <state>open</state>
      <tag>activerecord bug dependencies reload</tag>
      <title>AR::Base should not be nuking it's children, just because it lost interest</title>
      <updated-at type="datetime">2009-04-06T16:22:25+01:00</updated-at>
      <user-id type="integer">53176</user-id>
      <user-name>Shak</user-name>
      <creator-name>Tom Lea</creator-name>
      <url>http://rails.lighthouseapp.com/projects/8994/tickets/1339</url>
      <milestone-title>2.x</milestone-title>
      <assigned-user-name>Jeremy Kemper</assigned-user-name>
    </version>
    <version type="Ticket::Version">
      <assigned-user-id type="integer">85</assigned-user-id>
      <attachments-count type="integer">4</attachments-count>
      <body>Just to make it clear for people who encounter the exception and end up reading about it here:

- The exception is raised when ActiveRecord::Base#skip_time_zone_conversion_for_attributes is being evaluated, but that is only a symptom, so fixes that address only that specific attribute do not really address the real issue.

The actual issue is caused by ActiveSupport::Dependencies not reloading classes declared in files that are explicitly required. This means you should avoid explicitly require'ing files and make your dependencies automatic, by naming and placing the files so that the Dependencies module will load them automatically.

As for persistence of AR objects, as Tom Lea explained, the issue is really the fact that classes are *redefined* on each request, as opposed to having the same classes being *reloaded* on each request. A persisted object will always refer to the initial Class instance it was created with, whereas objects created on subsequent requests will refer to new classes (even if they have the same name).

That being said, if I had to persist AR objects, I would stick to a persistence mechanism that only serializes its scalar (or rather: literal) attributes (i.e. String, Fixnum, etc.) and which assigns those values to a new AR instance that gets reinitialized on each request, but I would never hold on to an actual Class reference.

I have to admit I am not familiar Marshall or MemStore and maybe that's the way those work. If that's the case, then it should pose no problem to use them for persistence.</body>
      <body-html>&lt;div&gt;&lt;p&gt;Just to make it clear for people who encounter the exception and
end up reading about it here:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The exception is raised when
ActiveRecord::Base#skip_time_zone_conversion_for_attributes is
being evaluated, but that is only a symptom, so fixes that address
only that specific attribute do not really address the real
issue.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The actual issue is caused by ActiveSupport::Dependencies not
reloading classes declared in files that are explicitly required.
This means you should avoid explicitly require'ing files and make
your dependencies automatic, by naming and placing the files so
that the Dependencies module will load them automatically.&lt;/p&gt;
&lt;p&gt;As for persistence of AR objects, as Tom Lea explained, the
issue is really the fact that classes are &lt;em&gt;redefined&lt;/em&gt; on
each request, as opposed to having the same classes being
&lt;em&gt;reloaded&lt;/em&gt; on each request. A persisted object will always
refer to the initial Class instance it was created with, whereas
objects created on subsequent requests will refer to new classes
(even if they have the same name).&lt;/p&gt;
&lt;p&gt;That being said, if I had to persist AR objects, I would stick
to a persistence mechanism that only serializes its scalar (or
rather: literal) attributes (i.e. String, Fixnum, etc.) and which
assigns those values to a new AR instance that gets reinitialized
on each request, but I would never hold on to an actual Class
reference.&lt;/p&gt;
&lt;p&gt;I have to admit I am not familiar Marshall or MemStore and maybe
that's the way those work. If that's the case, then it should pose
no problem to use them for persistence.&lt;/p&gt;&lt;/div&gt;</body-html>
      <closed type="boolean">false</closed>
      <created-at type="datetime">2009-04-06T18:24:14+01:00</created-at>
      <creator-id type="integer">28420</creator-id>
      <diffable-attributes type="yaml">--- {}

</diffable-attributes>
      <milestone-id type="integer">9903</milestone-id>
      <number type="integer">1339</number>
      <permalink>arbase-should-not-be-nuking-its-children-just-because-it-lost-interest</permalink>
      <priority type="integer">0</priority>
      <project-id type="integer">8994</project-id>
      <state>open</state>
      <tag>activerecord bug dependencies reload</tag>
      <title>AR::Base should not be nuking it's children, just because it lost interest</title>
      <updated-at type="datetime">2009-04-06T18:24:20+01:00</updated-at>
      <user-id type="integer">42669</user-id>
      <user-name>C. Bedard</user-name>
      <creator-name>Tom Lea</creator-name>
      <url>http://rails.lighthouseapp.com/projects/8994/tickets/1339</url>
      <milestone-title>2.x</milestone-title>
      <assigned-user-name>Jeremy Kemper</assigned-user-name>
    </version>
    <version type="Ticket::Version">
      <assigned-user-id type="integer">85</assigned-user-id>
      <attachments-count type="integer">4</attachments-count>
      <body>@Shak, @C. Bedard
If you must store between requests, Marshal it. If you would simply prefer it to be there between requests, stash it in your controller's class, or the model's class, these will be reloaded anyway. If you are using a cache store, and are hitting this issue, just don't use the memory store.

If you must marshal, don't worry, it's easy, everything works by 'magic' 99% of the time. Juts call Marshal.dump and Marshal.load as needed. Serialisation is handled for you, no need to define {,de}serializers, this is ruby, it uses its magic for good, not evil.</body>
      <body-html>&lt;div&gt;&lt;p&gt;@Shak, @C. Bedard If you must store between requests, Marshal
it. If you would simply prefer it to be there between requests,
stash it in your controller's class, or the model's class, these
will be reloaded anyway. If you are using a cache store, and are
hitting this issue, just don't use the memory store.&lt;/p&gt;
&lt;p&gt;If you must marshal, don't worry, it's easy, everything works by
'magic' 99% of the time. Juts call Marshal.dump and Marshal.load as
needed. Serialisation is handled for you, no need to define
{,de}serializers, this is ruby, it uses its magic for good, not
evil.&lt;/p&gt;&lt;/div&gt;</body-html>
      <closed type="boolean">false</closed>
      <created-at type="datetime">2009-04-06T20:24:12+01:00</created-at>
      <creator-id type="integer">28420</creator-id>
      <diffable-attributes type="yaml">--- {}

</diffable-attributes>
      <milestone-id type="integer">9903</milestone-id>
      <number type="integer">1339</number>
      <permalink>arbase-should-not-be-nuking-its-children-just-because-it-lost-interest</permalink>
      <priority type="integer">0</priority>
      <project-id type="integer">8994</project-id>
      <state>open</state>
      <tag>activerecord bug dependencies reload</tag>
      <title>AR::Base should not be nuking it's children, just because it lost interest</title>
      <updated-at type="datetime">2009-04-06T20:24:21+01:00</updated-at>
      <user-id type="integer">28420</user-id>
      <user-name>Tom Lea</user-name>
      <creator-name>Tom Lea</creator-name>
      <url>http://rails.lighthouseapp.com/projects/8994/tickets/1339</url>
      <milestone-title>2.x</milestone-title>
      <assigned-user-name>Jeremy Kemper</assigned-user-name>
    </version>
    <version type="Ticket::Version">
      <assigned-user-id type="integer">85</assigned-user-id>
      <attachments-count type="integer">4</attachments-count>
      <body>Thanks for the tips - I don't want to turn this into a support thread but just for completion: I'm trying to persist a semi static look up table created from an RPC call - a prime candidate for caching using Rails.cache. For the moment I'm using memstore (this may change to memcached later).

Since my extended instance methods (the lookup is a hash of tableless AR objects) are being stripped I've marshalled (without any fancy coding) them and it all seems to be working rather well. Thanks for the pointers.</body>
      <body-html>&lt;div&gt;&lt;p&gt;Thanks for the tips - I don't want to turn this into a support
thread but just for completion: I'm trying to persist a semi static
look up table created from an RPC call - a prime candidate for
caching using Rails.cache. For the moment I'm using memstore (this
may change to memcached later).&lt;/p&gt;
&lt;p&gt;Since my extended instance methods (the lookup is a hash of
tableless AR objects) are being stripped I've marshalled (without
any fancy coding) them and it all seems to be working rather well.
Thanks for the pointers.&lt;/p&gt;&lt;/div&gt;</body-html>
      <closed type="boolean">false</closed>
      <created-at type="datetime">2009-04-06T20:48:45+01:00</created-at>
      <creator-id type="integer">28420</creator-id>
      <diffable-attributes type="yaml">--- {}

</diffable-attributes>
      <milestone-id type="integer">9903</milestone-id>
      <number type="integer">1339</number>
      <permalink>arbase-should-not-be-nuking-its-children-just-because-it-lost-interest</permalink>
      <priority type="integer">0</priority>
      <project-id type="integer">8994</project-id>
      <state>open</state>
      <tag>activerecord bug dependencies reload</tag>
      <title>AR::Base should not be nuking it's children, just because it lost interest</title>
      <updated-at type="datetime">2009-04-06T20:48:52+01:00</updated-at>
      <user-id type="integer">53176</user-id>
      <user-name>Shak</user-name>
      <creator-name>Tom Lea</creator-name>
      <url>http://rails.lighthouseapp.com/projects/8994/tickets/1339</url>
      <milestone-title>2.x</milestone-title>
      <assigned-user-name>Jeremy Kemper</assigned-user-name>
    </version>
    <version type="Ticket::Version">
      <assigned-user-id type="integer">85</assigned-user-id>
      <attachments-count type="integer">4</attachments-count>
      <body>Note that you also see this error if you relate an AR model in a plugin to one that is not in a plugin.  It's bad practice, yes, but it exposes this error condition as well.</body>
      <body-html>&lt;div&gt;&lt;p&gt;Note that you also see this error if you relate an AR model in a
plugin to one that is not in a plugin. It's bad practice, yes, but
it exposes this error condition as well.&lt;/p&gt;&lt;/div&gt;</body-html>
      <closed type="boolean">false</closed>
      <created-at type="datetime">2009-04-08T00:34:30+01:00</created-at>
      <creator-id type="integer">28420</creator-id>
      <diffable-attributes type="yaml">--- {}

</diffable-attributes>
      <milestone-id type="integer">9903</milestone-id>
      <number type="integer">1339</number>
      <permalink>arbase-should-not-be-nuking-its-children-just-because-it-lost-interest</permalink>
      <priority type="integer">0</priority>
      <project-id type="integer">8994</project-id>
      <state>open</state>
      <tag>activerecord bug dependencies reload</tag>
      <title>AR::Base should not be nuking it's children, just because it lost interest</title>
      <updated-at type="datetime">2009-04-08T00:34:39+01:00</updated-at>
      <user-id type="integer">11769</user-id>
      <user-name>jmoses</user-name>
      <creator-name>Tom Lea</creator-name>
      <url>http://rails.lighthouseapp.com/projects/8994/tickets/1339</url>
      <milestone-title>2.x</milestone-title>
      <assigned-user-name>Jeremy Kemper</assigned-user-name>
    </version>
    <version type="Ticket::Version">
      <assigned-user-id type="integer">85</assigned-user-id>
      <attachments-count type="integer">4</attachments-count>
      <body>I'd just like to note that this bug is really prominent on Metals. Class data members et al collide into this bug and create a lot of hurt. (If I follow what's happening correctly, that is...)

Without workarounds, 2.3.2 Metals that hang on to things between requests are very hard to use in development mode as a result. You end up with some awkward calling conventions and/or a lot of Marshall stuff.

Please, Rails gods, find a fix for this. :)</body>
      <body-html>&lt;div&gt;&lt;p&gt;I'd just like to note that this bug is really prominent on
Metals. Class data members et al collide into this bug and create a
lot of hurt. (If I follow what's happening correctly, that
is...)&lt;/p&gt;
&lt;p&gt;Without workarounds, 2.3.2 Metals that hang on to things between
requests are very hard to use in development mode as a result. You
end up with some awkward calling conventions and/or a lot of
Marshall stuff.&lt;/p&gt;
&lt;p&gt;Please, Rails gods, find a fix for this. :)&lt;/p&gt;&lt;/div&gt;</body-html>
      <closed type="boolean">false</closed>
      <created-at type="datetime">2009-04-10T21:52:11+01:00</created-at>
      <creator-id type="integer">28420</creator-id>
      <diffable-attributes type="yaml">--- 
:title: AR::Base should not be nuking it's children, just because it lost interest
</diffable-attributes>
      <milestone-id type="integer">9903</milestone-id>
      <number type="integer">1339</number>
      <permalink>arbase-should-not-be-nuking-its-children-just-because-it-lost-interest</permalink>
      <priority type="integer">0</priority>
      <project-id type="integer">8994</project-id>
      <state>open</state>
      <tag>activerecord bug dependencies reload</tag>
      <title>AR::Base should not be nuking its children, just because it lost interest</title>
      <updated-at type="datetime">2009-04-10T21:52:16+01:00</updated-at>
      <user-id type="integer">53098</user-id>
      <user-name>maschnitz (at yahoo)</user-name>
      <creator-name>Tom Lea</creator-name>
      <url>http://rails.lighthouseapp.com/projects/8994/tickets/1339</url>
      <milestone-title>2.x</milestone-title>
      <assigned-user-name>Jeremy Kemper</assigned-user-name>
    </version>
    <version type="Ticket::Version">
      <assigned-user-id type="integer">85</assigned-user-id>
      <attachments-count type="integer">4</attachments-count>
      <body>Just tried the patch against 2.3, and I can't seem to find any evidence of a difference in memory leakynes anymore.

Could someone (ideally @C. Bedard with he same app) try the patch against master to confirm, or prove me stupid? The existing patch applies clean.

Thanks

p.s. Thanks for the grammar fix maschnitz&#9786;</body>
      <body-html>&lt;div&gt;&lt;p&gt;Just tried the patch against 2.3, and I can't seem to find any
evidence of a difference in memory leakynes anymore.&lt;/p&gt;
&lt;p&gt;Could someone (ideally @C. Bedard with he same app) try the
patch against master to confirm, or prove me stupid? The existing
patch applies clean.&lt;/p&gt;
&lt;p&gt;Thanks&lt;/p&gt;
&lt;p&gt;p.s. Thanks for the grammar fix maschnitz&amp;#9786;&lt;/p&gt;&lt;/div&gt;</body-html>
      <closed type="boolean">false</closed>
      <created-at type="datetime">2009-04-11T04:59:41+01:00</created-at>
      <creator-id type="integer">28420</creator-id>
      <diffable-attributes type="yaml">--- {}

</diffable-attributes>
      <milestone-id type="integer">9903</milestone-id>
      <number type="integer">1339</number>
      <permalink>arbase-should-not-be-nuking-its-children-just-because-it-lost-interest</permalink>
      <priority type="integer">0</priority>
      <project-id type="integer">8994</project-id>
      <state>open</state>
      <tag>activerecord bug dependencies reload</tag>
      <title>AR::Base should not be nuking its children, just because it lost interest</title>
      <updated-at type="datetime">2009-04-11T04:59:48+01:00</updated-at>
      <user-id type="integer">28420</user-id>
      <user-name>Tom Lea</user-name>
      <creator-name>Tom Lea</creator-name>
      <url>http://rails.lighthouseapp.com/projects/8994/tickets/1339</url>
      <milestone-title>2.x</milestone-title>
      <assigned-user-name>Jeremy Kemper</assigned-user-name>
    </version>
    <version type="Ticket::Version">
      <assigned-user-id type="integer">85</assigned-user-id>
      <attachments-count type="integer">4</attachments-count>
      <body>I saw this bug in an app that required some models from an initializer.  The second page load would die. This patch kinda-sorta fixed it, but if anyone's seeing this bug and can't figure out why, check your initializers for a `require 'user'`</body>
      <body-html>&lt;div&gt;&lt;p&gt;I saw this bug in an app that required some models from an
initializer. The second page load would die. This patch kinda-sorta
fixed it, but if anyone's seeing this bug and can't figure out why,
check your initializers for a &lt;code&gt;require 'user'&lt;/code&gt;&lt;/p&gt;&lt;/div&gt;</body-html>
      <closed type="boolean">false</closed>
      <created-at type="datetime">2009-08-13T01:22:01+01:00</created-at>
      <creator-id type="integer">28420</creator-id>
      <diffable-attributes type="yaml">--- {}

</diffable-attributes>
      <milestone-id type="integer">9903</milestone-id>
      <number type="integer">1339</number>
      <permalink>arbase-should-not-be-nuking-its-children-just-because-it-lost-interest</permalink>
      <priority type="integer">0</priority>
      <project-id type="integer">8994</project-id>
      <state>open</state>
      <tag>activerecord bug dependencies reload</tag>
      <title>AR::Base should not be nuking its children, just because it lost interest</title>
      <updated-at type="datetime">2009-08-13T01:22:08+01:00</updated-at>
      <user-id type="integer">13</user-id>
      <user-name>court3nay</user-name>
      <creator-name>Tom Lea</creator-name>
      <url>http://rails.lighthouseapp.com/projects/8994/tickets/1339</url>
      <milestone-title>2.x</milestone-title>
      <assigned-user-name>Jeremy Kemper</assigned-user-name>
    </version>
    <version type="Ticket::Version">
      <assigned-user-id type="integer">85</assigned-user-id>
      <attachments-count type="integer">4</attachments-count>
      <body>Guys, don't know if it helps, but we just found the similar issue to occur.

Having patched our Rails with the stuff you provided, we ran into the similar issue: we did something like

user.level.bar,

and sucked loudly, because belongs_to association (level on user) also used some class_inheritable_accessor (for scoping) and it also turned to become nil on and after 2nd FCKNG request.

The hint there is that our model, user, actually subclasses the model stored in the plugin. Do you have any hint on this?</body>
      <body-html>&lt;div&gt;&lt;p&gt;Guys, don't know if it helps, but we just found the similar
issue to occur.&lt;/p&gt;
&lt;p&gt;Having patched our Rails with the stuff you provided, we ran
into the similar issue: we did something like&lt;/p&gt;
&lt;p&gt;user.level.bar,&lt;/p&gt;
&lt;p&gt;and sucked loudly, because belongs_to association (level on
user) also used some class_inheritable_accessor (for scoping) and
it also turned to become nil on and after 2nd FCKNG request.&lt;/p&gt;
&lt;p&gt;The hint there is that our model, user, actually subclasses the
model stored in the plugin. Do you have any hint on this?&lt;/p&gt;&lt;/div&gt;</body-html>
      <closed type="boolean">false</closed>
      <created-at type="datetime">2009-08-13T17:24:01+01:00</created-at>
      <creator-id type="integer">28420</creator-id>
      <diffable-attributes type="yaml">--- {}

</diffable-attributes>
      <milestone-id type="integer">9903</milestone-id>
      <number type="integer">1339</number>
      <permalink>arbase-should-not-be-nuking-its-children-just-because-it-lost-interest</permalink>
      <priority type="integer">0</priority>
      <project-id type="integer">8994</project-id>
      <state>open</state>
      <tag>activerecord bug dependencies reload</tag>
      <title>AR::Base should not be nuking its children, just because it lost interest</title>
      <updated-at type="datetime">2009-08-13T17:24:08+01:00</updated-at>
      <user-id type="integer">51446</user-id>
      <user-name>mail2lf (at gmail)</user-name>
      <creator-name>Tom Lea</creator-name>
      <url>http://rails.lighthouseapp.com/projects/8994/tickets/1339</url>
      <milestone-title>2.x</milestone-title>
      <assigned-user-name>Jeremy Kemper</assigned-user-name>
    </version>
    <version type="Ticket::Version">
      <assigned-user-id type="integer">85</assigned-user-id>
      <attachments-count type="integer">4</attachments-count>
      <body>Never mind the previous comment, found require in the environment.rb. Sorry for senseless note.</body>
      <body-html>&lt;div&gt;&lt;p&gt;Never mind the previous comment, found require in the
environment.rb. Sorry for senseless note.&lt;/p&gt;&lt;/div&gt;</body-html>
      <closed type="boolean">false</closed>
      <created-at type="datetime">2009-08-14T13:51:06+01:00</created-at>
      <creator-id type="integer">28420</creator-id>
      <diffable-attributes type="yaml">--- {}

</diffable-attributes>
      <milestone-id type="integer">9903</milestone-id>
      <number type="integer">1339</number>
      <permalink>arbase-should-not-be-nuking-its-children-just-because-it-lost-interest</permalink>
      <priority type="integer">0</priority>
      <project-id type="integer">8994</project-id>
      <state>open</state>
      <tag>activerecord bug dependencies reload</tag>
      <title>AR::Base should not be nuking its children, just because it lost interest</title>
      <updated-at type="datetime">2009-08-14T13:51:13+01:00</updated-at>
      <user-id type="integer">51446</user-id>
      <user-name>mail2lf (at gmail)</user-name>
      <creator-name>Tom Lea</creator-name>
      <url>http://rails.lighthouseapp.com/projects/8994/tickets/1339</url>
      <milestone-title>2.x</milestone-title>
      <assigned-user-name>Jeremy Kemper</assigned-user-name>
    </version>
    <version type="Ticket::Version">
      <assigned-user-id type="integer">85</assigned-user-id>
      <attachments-count type="integer">4</attachments-count>
      <body>I'm facing a similar issue, and I think I may have found a use case that may help.

In two separate projects I'm working on, I have added an `attr_accessor` to the model for virtual attributes. The first is `data` and the second is `number`. Here are the implementations simplified:

@@@
class Forecast &lt; ActiveRecord::Base
  attr_accessor :data
end

class CreditCardProfile &lt; ActiveRecord::Base
  attr_accessor :number
end
@@@

There are used virtually when the object is initialised to hold data which will not be present when the object is saved to the database. In both cases, using instance variables such as `data` and `data=` have resulted in the following error:

    &gt;&gt; credit_card_profile.save
    NoMethodError: You have a nil object when you didn't expect it!
    You might have expected an instance of Array.
    The error occurred while evaluating nil.include?
        from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.3/lib/active_record/attribute_methods.rb:142:in `create_time_zone_conversion_attribute?'
        from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.3/lib/active_record/attribute_methods.rb:75:in `define_attribute_methods'
        from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.3/lib/active_record/attribute_methods.rb:71:in `each'
        from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.3/lib/active_record/attribute_methods.rb:71:in `define_attribute_methods'
        from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.3/lib/active_record/attribute_methods.rb:242:in `method_missing'
        from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.3/lib/active_record/transactions.rb:206:in `rollback_active_record_state!'
        from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.3/lib/active_record/transactions.rb:196:in `save'
        from (irb):98

It looks as though this only affects models with virtual attributes such as these. Looking at line 142 for attribute_methods.rb, since `[:datestamp, :timestamp]` are already defined, I think its choking around the `skip_time_zone_conversion_for_attributes` method.

I hope this helps!</body>
      <body-html>&lt;div&gt;&lt;p&gt;I'm facing a similar issue, and I think I may have found a use
case that may help.&lt;/p&gt;
&lt;p&gt;In two separate projects I'm working on, I have added an
&lt;code&gt;attr_accessor&lt;/code&gt; to the model for virtual attributes. The
first is &lt;code&gt;data&lt;/code&gt; and the second is &lt;code&gt;number&lt;/code&gt;.
Here are the implementations simplified:&lt;/p&gt;
&lt;pre&gt;
&lt;code&gt;class Forecast &amp;lt; ActiveRecord::Base
  attr_accessor :data
end

class CreditCardProfile &amp;lt; ActiveRecord::Base
  attr_accessor :number
end&lt;/code&gt;
&lt;/pre&gt;
&lt;p&gt;There are used virtually when the object is initialised to hold
data which will not be present when the object is saved to the
database. In both cases, using instance variables such as
&lt;code&gt;data&lt;/code&gt; and &lt;code&gt;data=&lt;/code&gt; have resulted in the
following error:&lt;/p&gt;
&lt;pre&gt;
&lt;code&gt;&amp;gt;&amp;gt; credit_card_profile.save
NoMethodError: You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.include?
    from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.3/lib/active_record/attribute_methods.rb:142:in `create_time_zone_conversion_attribute?'
    from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.3/lib/active_record/attribute_methods.rb:75:in `define_attribute_methods'
    from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.3/lib/active_record/attribute_methods.rb:71:in `each'
    from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.3/lib/active_record/attribute_methods.rb:71:in `define_attribute_methods'
    from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.3/lib/active_record/attribute_methods.rb:242:in `method_missing'
    from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.3/lib/active_record/transactions.rb:206:in `rollback_active_record_state!'
    from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.3/lib/active_record/transactions.rb:196:in `save'
    from (irb):98&lt;/code&gt;
&lt;/pre&gt;
&lt;p&gt;It looks as though this only affects models with virtual
attributes such as these. Looking at line 142 for
attribute_methods.rb, since &lt;code&gt;[:datestamp, :timestamp]&lt;/code&gt;
are already defined, I think its choking around the
&lt;code&gt;skip_time_zone_conversion_for_attributes&lt;/code&gt; method.&lt;/p&gt;
&lt;p&gt;I hope this helps!&lt;/p&gt;&lt;/div&gt;</body-html>
      <closed type="boolean">false</closed>
      <created-at type="datetime">2009-09-08T19:02:10+01:00</created-at>
      <creator-id type="integer">28420</creator-id>
      <diffable-attributes type="yaml">--- {}

</diffable-attributes>
      <milestone-id type="integer">9903</milestone-id>
      <number type="integer">1339</number>
      <permalink>arbase-should-not-be-nuking-its-children-just-because-it-lost-interest</permalink>
      <priority type="integer">0</priority>
      <project-id type="integer">8994</project-id>
      <state>open</state>
      <tag>activerecord bug dependencies reload</tag>
      <title>AR::Base should not be nuking its children, just because it lost interest</title>
      <updated-at type="datetime">2009-09-08T19:02:16+01:00</updated-at>
      <user-id type="integer">27299</user-id>
      <user-name>Josh Delsman</user-name>
      <creator-name>Tom Lea</creator-name>
      <url>http://rails.lighthouseapp.com/projects/8994/tickets/1339</url>
      <milestone-title>2.x</milestone-title>
      <assigned-user-name>Jeremy Kemper</assigned-user-name>
    </version>
    <version type="Ticket::Version">
      <assigned-user-id type="integer" nil="true"></assigned-user-id>
      <attachments-count type="integer">4</attachments-count>
      <body>I ran in to this problem when moving some models in to an 'engine' plugin. It occurs when I try and access a 'belongs_to' association in either the view or console.

(eg. @document.author.name)

# /app/model/user.rb
class User
  has_many :documents
end

# /app/vendor/qwerty/app/models/document.rb
class Document
  belongs_to  :author,  :class_name =&gt; 'User', :foreign_key =&gt; 'author_id'
end</body>
      <body-html>&lt;div&gt;&lt;p&gt;I ran in to this problem when moving some models in to an
'engine' plugin. It occurs when I try and access a 'belongs_to'
association in either the view or console.&lt;/p&gt;
&lt;p&gt;(eg. @document.author.name)&lt;/p&gt;
&lt;h1&gt;/app/model/user.rb&lt;/h1&gt;
&lt;p&gt;class User&lt;br&gt;
has_many :documents end&lt;/p&gt;
&lt;h1&gt;/app/vendor/qwerty/app/models/document.rb&lt;/h1&gt;
&lt;p&gt;class Document&lt;br&gt;
belongs_to :author, :class_name =&amp;gt; 'User', :foreign_key =&amp;gt;
'author_id' end&lt;/p&gt;&lt;/div&gt;</body-html>
      <closed type="boolean">false</closed>
      <created-at type="datetime">2009-09-23T19:08:12+01:00</created-at>
      <creator-id type="integer">28420</creator-id>
      <diffable-attributes type="yaml">--- 
:assigned_user: 85
</diffable-attributes>
      <milestone-id type="integer">9903</milestone-id>
      <number type="integer">1339</number>
      <permalink>arbase-should-not-be-nuking-its-children-just-because-it-lost-interest</permalink>
      <priority type="integer">0</priority>
      <project-id type="integer">8994</project-id>
      <state>open</state>
      <tag>activerecord bug dependencies reload</tag>
      <title>AR::Base should not be nuking its children, just because it lost interest</title>
      <updated-at type="datetime">2009-09-23T19:08:25+01:00</updated-at>
      <user-id type="integer">70786</user-id>
      <user-name>Kris Leech</user-name>
      <creator-name>Tom Lea</creator-name>
      <url>http://rails.lighthouseapp.com/projects/8994/tickets/1339</url>
      <milestone-title>2.x</milestone-title>
      <assigned-user-name nil="true"></assigned-user-name>
    </version>
    <version type="Ticket::Version">
      <assigned-user-id type="integer" nil="true"></assigned-user-id>
      <attachments-count type="integer">4</attachments-count>
      <body>I have this issue when using acts_as_versioned in rails 2.3.4 (also previously in 2.3.2).

This works:
Profile.first.updater.login
This blows up on create_time_zone_conversion_attribute?:
Profile.first.versions.first.updater.login

Note that the version actually returns a Profile::Version, not Profile, which is a subclass of Profile.

Also, Profile.first.versions.first.updater returns just fine AND (with inspect) shows the 'login' field normally. But somehow calling .login on it breaks.

I added the config.active_record.default_timezone line and that no longer happens... on the *first* hit of the server. Instead, it gives this error:

time_ago_in_words Profile.first.version.first.updated_at
=&gt; undefined method `abs' for Sat May 23 09:15:57 -0700 1970:Time
@ vendor/rails/actionpack/lib/action_view/helpers/date_helper.rb:62:in `distance_of_time_in_words'

Note that Profile.first.version.first.updated_at appears to work fine in all cases.

On subsequent hits, the create_time_zone_conversion_attribute? error happens again.

This behavior is identical between dev and production mode, and my cache_classes settings are normal (off for dev, on for prod).</body>
      <body-html>&lt;div&gt;&lt;p&gt;I have this issue when using acts_as_versioned in rails 2.3.4
(also previously in 2.3.2).&lt;/p&gt;
&lt;p&gt;This works:&lt;br&gt;
Profile.first.updater.login&lt;br&gt;
This blows up on create_time_zone_conversion_attribute?:&lt;br&gt;
Profile.first.versions.first.updater.login&lt;/p&gt;
&lt;p&gt;Note that the version actually returns a Profile::Version, not
Profile, which is a subclass of Profile.&lt;/p&gt;
&lt;p&gt;Also, Profile.first.versions.first.updater returns just fine AND
(with inspect) shows the 'login' field normally. But somehow
calling .login on it breaks.&lt;/p&gt;
&lt;p&gt;I added the config.active_record.default_timezone line and that
no longer happens... on the &lt;em&gt;first&lt;/em&gt; hit of the server.
Instead, it gives this error:&lt;/p&gt;
&lt;p&gt;time_ago_in_words Profile.first.version.first.updated_at&lt;br&gt;
=&amp;gt; undefined method &lt;code&gt;abs' for Sat May 23 09:15:57 -0700
1970:Time @
vendor/rails/actionpack/lib/action_view/helpers/date_helper.rb:62:in&lt;/code&gt;distance_of_time_in_words'&lt;/p&gt;
&lt;p&gt;Note that Profile.first.version.first.updated_at appears to work
fine in all cases.&lt;/p&gt;
&lt;p&gt;On subsequent hits, the create_time_zone_conversion_attribute?
error happens again.&lt;/p&gt;
&lt;p&gt;This behavior is identical between dev and production mode, and
my cache_classes settings are normal (off for dev, on for
prod).&lt;/p&gt;&lt;/div&gt;</body-html>
      <closed type="boolean">false</closed>
      <created-at type="datetime">2009-10-22T05:13:22+01:00</created-at>
      <creator-id type="integer">28420</creator-id>
      <diffable-attributes type="yaml">--- {}

</diffable-attributes>
      <milestone-id type="integer">9903</milestone-id>
      <number type="integer">1339</number>
      <permalink>arbase-should-not-be-nuking-its-children-just-because-it-lost-interest</permalink>
      <priority type="integer">0</priority>
      <project-id type="integer">8994</project-id>
      <state>open</state>
      <tag>activerecord bug dependencies reload</tag>
      <title>AR::Base should not be nuking its children, just because it lost interest</title>
      <updated-at type="datetime">2009-10-22T05:13:29+01:00</updated-at>
      <user-id type="integer">73743</user-id>
      <user-name>Sai Emrys</user-name>
      <creator-name>Tom Lea</creator-name>
      <url>http://rails.lighthouseapp.com/projects/8994/tickets/1339</url>
      <milestone-title>2.x</milestone-title>
      <assigned-user-name nil="true"></assigned-user-name>
    </version>
  </versions>
  <attachments type="array">
    <attachment type="Attachment">
      <code>f836b795d298e32ff30bef2410ead136aefdfbdf</code>
      <content-type>text/plain</content-type>
      <created-at type="datetime">2008-11-06T21:36:17+00:00</created-at>
      <filename>prevent_class_method_and_variable_nukeing_on_AR_reset_subclasses.diff</filename>
      <height type="integer" nil="true"></height>
      <id type="integer">60054</id>
      <size type="integer">1091</size>
      <uploader-id type="integer">28420</uploader-id>
      <width type="integer" nil="true"></width>
      <url>http://rails.lighthouseapp.com/attachments/60054/prevent_class_method_and_variable_nukeing_on_AR_reset_subclasses.diff</url>
    </attachment>
    <attachment type="Attachment">
      <code>4418fd01da11c3394a11782a506bcd47ff406199</code>
      <content-type>text/plain</content-type>
      <created-at type="datetime">2009-01-06T19:13:49+00:00</created-at>
      <filename>AR_dev_mode_reloading.diff</filename>
      <height type="integer" nil="true"></height>
      <id type="integer">75832</id>
      <size type="integer">4355</size>
      <uploader-id type="integer">42669</uploader-id>
      <width type="integer" nil="true"></width>
      <url>http://rails.lighthouseapp.com/attachments/75832/AR_dev_mode_reloading.diff</url>
    </attachment>
    <attachment type="Attachment">
      <code>f60f28f39cb86b4552ecdb6ef5b0fc59a253b25f</code>
      <content-type>application/zip</content-type>
      <created-at type="datetime">2009-01-08T05:36:24+00:00</created-at>
      <filename>lighthouse-ticket-1339.zip</filename>
      <height type="integer" nil="true"></height>
      <id type="integer">76348</id>
      <size type="integer">94161</size>
      <uploader-id type="integer">42669</uploader-id>
      <width type="integer" nil="true"></width>
      <url>http://rails.lighthouseapp.com/attachments/76348/lighthouse-ticket-1339.zip</url>
    </attachment>
    <attachment type="Attachment">
      <code>9ccdd2a6117907cdc6e101e41ca96f67952dbf8c</code>
      <content-type>text/plain</content-type>
      <created-at type="datetime">2009-01-12T10:28:26+00:00</created-at>
      <filename>rebased-20090112-prevented-AR.reset_subclasses-from-killing-existing.patch</filename>
      <height type="integer" nil="true"></height>
      <id type="integer">77318</id>
      <size type="integer">1295</size>
      <uploader-id type="integer">28420</uploader-id>
      <width type="integer" nil="true"></width>
      <url>http://rails.lighthouseapp.com/attachments/77318/rebased-20090112-prevented-AR.reset_subclasses-from-killing-existing.patch</url>
    </attachment>
  </attachments>
</ticket>
