Search code examples
ruby-on-railsrubyruby-on-rails-4rails-engines

Rails engine model schema not updating


I currently have an engine within my rails application named Marketplace. I am creating a model to be used within the engine, however this model needs to interact with the main application's database.

I created the model, and added a bit of code to my engine.rb file so that when migrations are run from the main application, the engine's migration files are run, too.

initializer :append_migrations do |app|
    unless app.root.to_s.match root.to_s
        config.paths["db/migrate"].expanded.each do |expanded_path|
            app.config.paths["db/migrate"] << expanded_path
        end
    end
end

Everything works fine, and the database recognizes the engine, however the schema file for the model is not updated. I'm assuming the schema command cannot find the appropriate model to update.

Also, using rails 4.


Solution

  • Chances are, if you're using a rails engine then you probably don't want to update your Engine's model's schema. This is so you could theoretically use the engine across different applications. From the rails documentation, I've learned to create the migrations within my rails application and then copy the migrations into the application with:

    rake engine:install:migrations
    

    Then, you can run the migrations from your rails application.

    Rails guide on engine migrations

    While the previous written code will in fact run the migrations from your application's engine, it will not copy the migration files into your application (and then what would be the point of the engine?).