Ruby load level

I have the following model in a Rails application:

class Comment
  class Digest
  end
end

When I try to load it in the console, I get:

$ rails c
Loading development environment (Rails 4.1.2)
irb(main):001:0> Comment::Digest
(irb):1: warning: toplevel constant Digest referenced by Comment::Digest
=> Digest
irb(main):002:0> require "comment/digest"
=> true
irb(main):003:0> Comment::Digest
=> Comment::Digest

If I change the name Digestto Other, it works fine. The class is designed for digest notifications, and I would like to keep the name if I can get around it relatively easily.

class Comment
  class Other
  end
end

irb(main):003:0> Comment::Other
=> Comment::Other

If I add an initializer to load the model, it works fine:

# config/initializers/comment_digest_loader.rb

require "comment"
require "comment/digest"

I understand that it does not load using const_getb / c. Digest module already exists. Just not sure what the best way to handle this.

+4
source share
2 answers

, , , , , :

Comment::String
# warning: toplevel constant String referenced by Comment::String

, Comment::Digest , .


Update:

, Rail const_missing, Digest , const_misssing, -, , .

, , Digest.

, :

Rails.application.eager_load!
+3

, ruby ​​ . Comment::Digest comment.

, .

require "comment/digest"

Comment::Digest

, Comment::Digest. ruby ​​ Digest

0

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


All Articles