mc9
mc9

Reputation: 6349

Rails engine: get parent app's name

I would like to know if a mountable Rails engine can retrieve the name of the parent application on which it is mounted.

I have looked through the official Rails engine guide, but could not find anything. Any sources or ideas?

Upvotes: 3

Views: 1255

Answers (2)

bovender
bovender

Reputation: 1960

As of 2021, with Rails 6.x/Ruby 2.7, class does not have a method #parent.

I use:

::Rails.application.class.module_parent

to obtain the (technical) name of an application that my engine is mounted on.

Upvotes: 5

Pierre Pretorius
Pierre Pretorius

Reputation: 2909

I'm surprised no one answered this in more than a year. You can use:

Rails.application.class.parent.name

Depending on where you use it you might need to make sure you start at the root rails module which makes this a safer choice:

::Rails.application.class.parent.name

Upvotes: 6

Related Questions