Topic: access by association breaks when I set config.cache_class = true

Have this weird issue that causes exceptions when I try to access an object through an association in a prod environment. I have been able to re-create the issue in either prod/dev configuration easily by flipping the config.cache_class flag.  When the flag is set to true, accessing a simple belongs_to association returns the following stack trace:

1.9.2-p318 :001 > u=User.find(1)
  User Load (6.1ms)  SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1  [["id", 1]]
 => #<User id: 1, email: "", encrypted_password: "$2a$10$MC44cekdIfWJvDgQFfPXquuQt0uqsZL7OVth8ifLK/C/...", account_id: 1, telephone_number: "444-444-4444", authorized_by_account_mgr: false, authorization_changed_at: nil, reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 2, current_sign_in_at: "2012-08-02 06:36:55", last_sign_in_at: "2012-08-02 06:26:39", current_sign_in_ip: "", last_sign_in_ip: "", created_at: "2012-08-02 06:24:46", updated_at: "2012-08-02 06:36:55"> 
1.9.2-p318 :002 > u.account
NoMethodError: undefined method `zero?' for nil:NilClass
  from /Users/tm/.rvm/gems/ruby-1.9.2-p318/gems/activerecord-3.2.5/lib/active_record/associations/alias_tracker.rb:30:in `aliased_name_for'
  from /Users/tm/.rvm/gems/ruby-1.9.2-p318/gems/activerecord-3.2.5/lib/active_record/associations/alias_tracker.rb:18:in `aliased_table_for'
  from /Users/tm/.rvm/gems/ruby-1.9.2-p318/gems/activerecord-3.2.5/lib/active_record/associations/join_helper.rb:15:in `block in construct_tables'
  from /Users/tm/.rvm/gems/ruby-1.9.2-p318/gems/activerecord-3.2.5/lib/active_record/associations/join_helper.rb:14:in `each'
  from /Users/tm/.rvm/gems/ruby-1.9.2-p318/gems/activerecord-3.2.5/lib/active_record/associations/join_helper.rb:14:in `construct_tables'
  from /Users/tm/.rvm/gems/ruby-1.9.2-p318/gems/activerecord-3.2.5/lib/active_record/associations/association_scope.rb:37:in `add_constraints'
  from /Users/tm/.rvm/gems/ruby-1.9.2-p318/gems/activerecord-3.2.5/lib/active_record/associations/association_scope.rb:31:in `scope'
  from /Users/tm/.rvm/gems/ruby-1.9.2-p318/gems/activerecord-3.2.5/lib/active_record/associations/association.rb:98:in `association_scope'
  from /Users/tm/.rvm/gems/ruby-1.9.2-p318/gems/activerecord-3.2.5/lib/active_record/associations/association.rb:87:in `scoped'
  from /Users/tm/.rvm/gems/ruby-1.9.2-p318/gems/activerecord-3.2.5/lib/active_record/associations/singular_association.rb:42:in `find_target'
  from /Users/tm/.rvm/gems/ruby-1.9.2-p318/gems/activerecord-3.2.5/lib/active_record/associations/association.rb:145:in `load_target'
  from /Users/tm/.rvm/gems/ruby-1.9.2-p318/gems/activerecord-3.2.5/lib/active_record/associations/association.rb:55:in `reload'
  from /Users/tm/.rvm/gems/ruby-1.9.2-p318/gems/activerecord-3.2.5/lib/active_record/associations/singular_association.rb:9:in `reader'
  from /Users/tm/.rvm/gems/ruby-1.9.2-p318/gems/activerecord-3.2.5/lib/active_record/associations/builder/association.rb:44:in `block in define_readers'
  from (irb):2
  from /Users/tm/.rvm/gems/ruby-1.9.2-p318/gems/railties-3.2.5/lib/rails/commands/console.rb:47:in `start'
  from /Users/tm/.rvm/gems/ruby-1.9.2-p318/gems/railties-3.2.5/lib/rails/commands/console.rb:8:in `start'
  from /Users/tm/.rvm/gems/ruby-1.9.2-p318/gems/railties-3.2.5/lib/rails/commands.rb:41:in `<top (required)>'
  from script/rails:6:in `require'
  from script/rails:6:in `<main>'1.9.2-p318 :003 > 

I can reproduce it with a bare bones User class

class User < ActiveRecord::Base
  attr_accessible :email, :password, :password_confirmation, :remember_me, :account, :telephone_number, :authorized_by_account_mgr, :authorization_changed_at, :account_id

  belongs_to :account

For completeness, I am including the Account class

class Account < ActiveRecord::Base
  store :preferences, accessors: [:account_legal_name, :account_legal_name_color, :report_text_color, :report_font, :legal_disclaimer]

This is running on postgress version 9.1.3.
I can reproduce this on both my Mac and Ubuntu 10.4.

I must be doing something wrong, but I am out of ideas.

Thanks much for your help,


Last edited by takis (2012-08-02 14:29:17)

Re: access by association breaks when I set config.cache_class = true

Have you found a solution?

I originally developed my app with Ruby 1.8.7 and Rails 3.2.6, but needed to upgrade to Ruby 1.9.2.  After upgrading to Ruby 1.9.2, I am now getting an error "NoMethodError: undefined method `zero?' for :+:Symbol" from alias_tracker.rb.

The error is thrown everytime it tries to do this type of query:

This is basic Rails!  Can't find anything about this error...  I switched back to Ruby 1.8.7 and the app is working again, except I need to the new Ruby for a gem.

Last edited by cmentch (2012-11-09 17:25:34)

Re: access by association breaks when I set config.cache_class = true

My problem was caused by my defining a method forwarding method for Array class that apparently interfered with some library.
Since in development mode all classes are loaded on every request, my code worked fine; obviously, cacheing classes led to the conflict manifesting at some point in my execution once the conflicting library was loaded.

I still don't know which library was the issue, but I had to change my code to create a subclass of array instead of array.