How is persistent search resolved when two files are mutually dependent on each other?
I have models A
and B
, and two versions of constants Foo
and Bar
with names in each model:
app / models / a.rb
class A < ActiveRecord::Base
FOO = "foo in A"
BAR = B::FOO
end
app / models / b.rb
class B < ActiveRecord::Base
FOO = "foo in B"
BAR = A::FOO
end
It A::BAR
depends on B::FOO
, which is defined in the file b.rb
, which has a definition B::BAR
, which depends on A::FOO
, which is defined in the file a.rb
that has a definition A::BAR
. However, I can call A::BAR
without error:
A::BAR # => "foo in B"
It looks like a loop to me, and I can't imagine any order or way in which these two files are loaded without falling into infinite recursion.
How is persistent search resolved on a call A::BAR
?
I also found out that if I call B::BAR
the very beginning, before calling A::BAR
, then it generates an error that says that the alphabetical order of file names a.rb
and b.rb
associated with this problem. This also begs the question: if a file upload is triggered with a constant resolution, then that order between files shouldn't matter. On the other hand, if the files are loaded before these constants are called, then it shouldn't matter at A::BAR
first for calling first or B::BAR
. What's going on here?
I also found that if I changed the order of the definitions to the following,
app / models / a.rb
class A < ActiveRecord::Base
BAR = B::FOO
FOO = "foo in A"
end
app / models / b.rb
class B < ActiveRecord::Base
BAR = A::FOO
FOO = "foo in B"
end
then either calling A::BAR
first or calling B::BAR
first will cause an error. I wonder how this is related.
source to share
No one has answered this question yet
Check out similar questions: