使用Redmine插件更改现有模型

时间:2009-11-11 11:17:07

标签: ruby-on-rails plugins redmine

Redmine插件教程解释了如何包装核心模型,但我需要的是将另一列添加到journals表中。 我需要在日记帐模型中插入一个布尔字段。使用'belongs_to:journal'关系创建另一个模型似乎有点矫枉过正。 这可以用插件完成吗? 我应该注意到我是铁杆新手。

2 个答案:

答案 0 :(得分:3)

您只需要创建适当的migration

在插件目录中,使用以下内容创建文件db/migrate/update_journal.rb

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

然后,您可以执行任务rake db:migrate_plugins RAILS_ENV=production以使用新字段更新数据库。

执行迁移后,您的日记数据库将拥有您可以像其他所有字段一样调用的my_bool字段。

答案 1 :(得分:0)

我能够使用以下代码扩展现有的用户模型:

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

此外,我需要以一个数字开头的方式命名迁移文件,例如。为myplugin /分贝/迁移/ 001_update_user.rb