Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating models and migrations for an engine

So I'm working on a new project that seems like a great choice for using the new Engine functionality. It's as engines say, its own little app, w/ its own views and controllers and models. Here's where I'm coming up short.

I create my test application in which I will mount the new engine.

rails new engine_app && cd engine_app

I then create the new engine

rails plugin new my_engine --mountable

I then add the 'gem' to the engine_app's gemfile

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

I then mount the engine in engine_app's routes as so

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

I then cd into my_engine's dummy app and run

rails generate model MyModel title:string body:text

Here's where I run into my confusion. From what I understand this is supposed to generate a namespace table (I think it would be my_engine_my_model). The table in the migration file is just my_model.

Secondly how do I run this migration and is the migration file correct in only calling the table :my_model? I have tried running the following but nothing seems to happen, and I've checked the database and the table isn't there.

So to recap, I need to know how to create migrations in the engine, and be able to run them on the parent apps database correctly.

Thanks for any help and guidance.

like image 471
kwbock Avatar asked Oct 04 '12 00:10

kwbock


People also ask

What are migrations in Rails?

A Rails migration is a tool for changing an application's database schema. Instead of managing SQL scripts, you define database changes in a domain-specific language (DSL). The code is database-independent, so you can easily move your app to a new platform.

How does a Rails engine work?

Rails looks first in the application's ( test/dummy ) app/views directory and then in the engine's app/views directory. When it can't find it, it will throw this error. The engine knows to look for blorgh/comments/_comment because the model object it is receiving is from the Blorgh::Comment class.


1 Answers

So all of the tutorials I read didn't specify that you needed to run script/rails generate from within the root level of your engine. I kept seeing references telling me to goto the test/dummy app. After running script/rails generate model [fields] from the root of my engine it created the appropriate model, migration rake task and i was able to run

rake my_engine:install:migrations; rake db:migrate

to run the migrations

like image 112
kwbock Avatar answered Sep 21 '22 22:09

kwbock