题
Redmine插件教程说明了如何包装核心模型,但是我需要的是将另一列添加到日记表中。我需要在期刊模型中插入的布尔字段。用“ allys_to:journal”关系创建另一个模型似乎是一个过度杀伤。可以使用插件完成吗?我应该注意,我是Rails Newbie。
解决方案
您只需要创建适当的 移民.
在插件的目录中,创建文件 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
您将能够像其他所有字段一样呼叫的字段。
其他提示
我能够使用以下代码扩展现有用户模型:
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
不隶属于 StackOverflow