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.

+3
ruby-on-rails constants


source to share


No one has answered this question yet

Check out similar questions:

203
Ruby on Rails: Where to Define Global Constants?
6
i18n to Ruby on Rails, <and> is replaced with & gt; & lt; when not intended
2
Is there a fundamental hurdle for Rails making "ActiveRecord.where" type safe "belongs"?
1
Rails 4 constants - "Unitialized constant" error
1
The order of the rails of the association
1
Errors are shown twice in rails
1
Is the "uninitialized constant" error related to the class loading order?
1
When control_name.classify.constantize is called I got # <nomethoderror: undefined method 'belongs_to' for relatorio: class> "
0
Rails 4 undefined public method for ActiveModel
0
Rails autoloading and gem classes



All Articles
Loading...
X
Show
Funny
Dev
Pics