Alter existing model with a Redmine plugin
The Redmine plugin tutorials explain how to wrap core models but what I need is to add another column to the journals table. I开发者_如何转开发 need a boolean field inserted in the journals model. Creating another model with a 'belongs_to :journal' relation seems like an overkill. Can this be done with a plugin? I should note that I am a rails newbie.
You just have to create the appropriate migration.
In your plugin's directory, create the file db/migrate/update_journal.rb
with the following :
class UpdateJournal < ActiveRecord::Migration
def self.up
change_table :journal do |t|
t.column :my_bool, :boolean
end
end
def self.down
change_table :journal do |t|
t.remove :my_bool
end
end
end
Then you can execute the task rake db:migrate_plugins RAILS_ENV=production
to update your database with the new field.
After executing the migration, your journal database will have the my_bool
field that you'll be able to call like every other field.
I was able to extend the existing user model using the following code:
class UpdateUsers < ActiveRecord::Migration
def up
add_column :users, :your_new_column, :string, :default => ''
add_column :users, :your_other_new_column, :string, :default => ''
end
def down
remove_column :users, :your_new_column
remove_column :users, :your_other_new_column
end
end
Also I needed to name the migration file in way that it began with a number eg. myplugin/db/migrate/001_update_user.rb
精彩评论