Creating models and migrations for the engine

So, I'm working on a new project that seems like a great choice to use the new Engine functionality. This, as the engines say, is its own application, with its own representations and controllers and models. Here, where I am a little behind.

I create my test application in which I can install a new engine.

rails new engine_app && cd engine_app 

Then create a new engine

 rails plugin new my_engine --mountable 

Then I add "gem" to the gemfile engine_app

 gem 'my_engine', :path => './my_engine' 

Then I mount the engine in the engine_app routes so

 mount MyEngine::Engine, :at => '/my_engine' 

Then I write cd to my_engine dummy app and run

 rails generate model MyModel title:string body:text 

Here, where I run into confusion. From what I understand, this should create a namespace table (I think it will be my_engine_my_model). The table in the migration file is just my_model.

Secondly, how do I perform this migration and is the migration file valid only for calling the table: my_model? I tried to run the following, but nothing happens, and I checked the database and the table is not there.

So, to understand, I need to know how to create migrations in the engine, and be able to correctly run them in the database of the parent applications.

Thanks for any help and recommendations.

+4
source share
1 answer

So, all the tutorials read did not indicate that you need to run script / rails from the root level of your engine. I continued to see links telling me to go to the test / dummy application. After running script / rails, generate the [field] model from the root of my engine, it created a suitable model, the rake migration task and I was able to run

 rake my_engine:install:migrations; rake db:migrate 

to start the migration

+4
source

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


All Articles