Reputation: 2872
Yesterday I posted this question with some strange autloading issue: https://stackoverflow.com/posts/comments/83451042?noredirect=1
It seems that I kind of found the reason behind this, my actual code looks something like:\
a.rb
require_relative './b/b.rb'
module A
def self.abc content
return A::B.new
end
end
B has nothing fancy:
b.rb
module A
class B
def initialize
end
end
class C < B
def initialize
super
end
end
end
When I start my application, I can run A::C
in console and find the class. After I reload!
, A::C
gives a uninitialized constant
. When I then call A::B
and shortly after A::C
it works again.
So I guess I'm not able to have class C to reside in the same 'file' as B. Since C is closely related to B I thought it made sense to keep this comparable code together. Am I thinking wrongly here, or should I restructure this into something different?
Kind of new to working with classes outside the normal Rails stuff. Thanks.
Upvotes: 0
Views: 56
Reputation: 2624
When I start my application, I can run A::C in console and find the class. After I reload!, A::C gives a uninitialized constant. When I then call A::B and shortly after A::C it works again.
Because rails console
is not always as same as reload!
. You can test this by configuring cache_classes
to be true
in environments/development.rb
. You'll no longer get the error.
But you should follow the Rails's naming convention to not configure anymore, even require_relative
.
The final structure:
lib
-- my_lib
---- a
------ b.rb
------ c.rb
---- a.rb
Don't forget to add your lib my_lib
to autoload_paths
!
Upvotes: 1