How to create a model in the init.rb plugin?

How to properly initialize Redmine plugins that need to create a model (read the database record) defined in the plugin itself?

For example, I have my_redmine_plugin plugin that comes with the MyPluginModel model. When initializing the plugin, I would like

  • read MyPluginModel entry from DB
  • run some initialization code with the entry

Given the following code:

 require 'redmine' Redmine::Plugin.register :my_redmine_plugin do name 'My Redmine Plugin' # ... end Rails.configuration.to_prepare do m = MyPluginModel.find(1) run_some_init_code(m) end 

It looks like to_prepare starts before the migration:

 $ bundle exec rake redmine:plugins:migrate NAME=my_redmine_plugin `table_structure': Could not find table 'mypluginmodel' (ActiveRecord::StatementInvalid) ... 

When I comment on the to_prepare block during the migration, everything works fine. Is there any way to detect the migration process?

+6
source share
1 answer

Try using after_initialize instead of to_prepare . This is not a Redmine problem. The Rails initialization process has different hooks that start at different times during startup. See the API docs for more information.

Update: thinking about this, it may still not help with the migration - you could just get rid of this error on the go.

0
source

All Articles