Ruby class object force reload

785 views Asked by At

I am trying to reproduce a bug that happens when I get a record from ActiveRecord but the class(the activeRecord class, not the instance) is not in memory, then activeRecord class model is reloaded. I didn't caught the bug since it seems that class object remains in memory for quite some time.

Here is the test:

def test_decryption_should_work_at_any_time
    require 'pry'
    binding.pry 
    account = Account.last 
    require 'pry'
    binding.pry
end

Take a look and you'll see account is the same object no matter what I try:

   102:     f = Account.create!(:key => "secret", :password => "password")
 =>103:     binding.pry 
   104:     account = Account.last 
   105:     binding.pry
   106:   end
   107: end
[1] pry(#<ActiveRecordTest>)> f.class.object_id
=> 70278442281960

Then reload the object

  104:     account = Account.last 
 =>105:     binding.pry
   106:   end
   107: end
[1] pry(#<ActiveRecordTest>)> account.class.object_id
=> 70278442281960

Unfortunatelly the class object is getting the same object for the Account Class than when it was created. Is there a way to force this class to be reloaded?

I know where the bug is, I am just trying to reproduce it. So is there a way to unload that class?

1

There are 1 answers

0
Jesse Storimer On

Classes will not be reloaded in production Rails code. That's specifically a feature of Rails' development mode, and even then classes are only reloaded when they need to be, not on each inspection.

Does your Account model use STI (single table inheritance)? I've experienced issues like you're mentioning (classes being loaded at runtime in instances like that.