0

I'm a bit confused with namespacing in engines. In a Rails engine, where isolate_namespace is used,

module Blog
  class Engine < Rails::Engine
    isolate_namespace Blorgh
  end
end

when is it required that you refer to objects with the namespace Blog (e.g. Blog::Post vs just Post)?

As for example, within the controller of a Post resource of the engine, is it ok to just do Post.find? When it is absolutely required that you use Blog::Post?

Also in models associations, assume that Post has_many :comments. Somehow, I was expecting to define it as follows:

class Post < ActiveRecord::Base
  :has_many "blog/comments"
end

since everything is namespaced (models, table names, ...), but it looks like has_many :comments just works. Why namespacing is not used in association keys, and in the case where a Comment resource exists in the host applications, how does rails know which Comment I'm referring to?

Panagiotis Panagi
  • 9,927
  • 7
  • 55
  • 103

1 Answers1

2

When you're inside some module, you can refer to other member of the module without giving the module name, eg:

module Foo

  class Bar
    def initialize
      @baz = Baz.new # same as Foo::Baz.new
    end
  end
  class Baz

  end
end

If Baz doesn't exist in the current module, it will cascade down to find the definition, eventually calling const_missing (upon which is built the autoload of classes in Rails), then throw an error if nothing is found.

Rest of your questions is answered here.

apneadiving
  • 114,565
  • 26
  • 219
  • 213