【问题标题】:Alter existing model with a Redmine plugin使用 Redmine 插件更改现有模型
【发布时间】:2009-11-11 11:17:07
【问题描述】:

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

【问题讨论】:

    标签: ruby-on-rails plugins redmine


    【解决方案1】:

    您只需创建适当的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 字段,您可以像其他所有字段一样调用该字段。

    【讨论】:

    • 这种方式好像不行了。你还有什么办法吗?
    【解决方案2】:

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

    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/db/migrate/001_update_user.rb

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-27
      • 1970-01-01
      • 2022-07-12
      相关资源
      最近更新 更多