Active Record forgets methods! bug?

I have created a rails app. When in development mode, I hit a problem the second time I try to display a page from a particular controller: it raises a method_missing exception because suddenly the model I am trying to use has forgotten about all the methods I added to it, and all its association methods (from has_one and has_many). Inspecting the model shows that it still has it's @attributes array.

In production mode, it works fine. Same result on Rails 1.x and 2.0.2. Below is an example exception. In my DB, System has_many test_results.

Any ideas? Thanks, -Sam.

undefined method `test_results' for #<System:0x2521eb0> RAILS_ROOT: /Users/sam/Development/ideas/trunk/spierson/pbits/server/pbits2

Application Trace | Framework Trace | Full Trace /Library/Ruby/Gems/1.8/gems/activerecord-2.0.2/lib/active_record/attribute_methods.rb:205:in `method_missing' app/controllers/results_controller.rb:34:in `report' /Library/Ruby/Gems/1.8/gems/activerecord-2.0.2/lib/active_record/associations/association_proxy.rb:125:in `each' /Library/Ruby/Gems/1.8/gems/activerecord-2.0.2/lib/active_record/associations/association_proxy.rb:125:in `send' /Library/Ruby/Gems/1.8/gems/activerecord-2.0.2/lib/active_record/associations/association_proxy.rb:125:in `method_missing' /Library/Ruby/Gems/1.8/gems/activerecord-2.0.2/lib/active_record/associations/has_many_through_association.rb:133:in `method_missing_without_paginate' vendor/plugins/will_paginate/lib/will_paginate/finder.rb:93:in `method_missing' app/controllers/results_controller.rb:32:in `report' /Library/Ruby/Gems/1.8/gems/actionpack-2.0.2/lib/action_controller/base.rb:1158:in `send' <snip - boring part deleted>

This is because of the file loading problem in development mode. So for this, add the following statement on top of the 'system.rb' file.

  requrie 'test_result.rb'

Use the file names that you are using. I have given the file names in assumption.

I hope it will work. Because I have experienced the same problem.

- Karthik.

Many thanks for your suggestion Karthink. That was a good idea: require 'test_result' was indeed missing from system.rb

Unfortunately this did not solve the problem. Something more sinister is going on.


Sorry Karthink -> Karthik

I dont know what else the problem is.

Did you restart the server after adding the statement? If not, please try that also.

Karthi kn wrote:

Did you restart the server after adding the statement? If not, please try that also.

Yes. Twice :slight_smile:

Karthi kn wrote:

Can you show that controller code here?

No need - you cracked it! I went further and put in appropriate require statements for EVERY association in EVERY model. That fixed it. I have some 2-step joins (using :through) and I think one of those maybe was causing the problem.

Many thanks for your help!