Rails: A subclass is not registered as an instance of the parent class

In the Rails application I am working in, I have code similar to the following:

# app/models/a.rb class A < ActiveRecord::Base; ...; end # app/models/b.rb class B < A; ...; end # /app/elsewhere... do_case(B.new) def do_case(letter) case letter when A "not nil" end end 

When I run it locally, it does as expected, and I get non-zero as the return value. But I get nil in my test environment.

In the test environment, it is shown that A is among the ancestors of ( B.ancestors ) letter ( B.new ), but cannot be registered as a subclass with the case equality operator or is_a?(A) .

 A === B.new #=> false in test environment, true locally B.new.is_a?(A) #=> false in test environment, true locally 

This seems like a Rails startup problem, but I'm not sure why these methods will fail if the parent class is included in the ancestors in both environments. What's happening?

+5
source share
1 answer

Agree, this seems like a problem with startup. Although not ideal, you can resolve it by adding a call to require_dependency in B

As for the root problem, perhaps you have some dependencies in the library in the test environment that are not in Test. I would upload the REPL to Test and check A Some useful methods:

 class A def self.what_is_my_nesting Module.nesting end end A.what_is_my_nesting A.ancestors B.ancestors 

etc.

0
source

Source: https://habr.com/ru/post/1263409/


All Articles