在rails中在运行时向模型添加列(无需其他表)

我正在尝试为我的Web应用程序的管理员提供向模型添加一些新字段的function。 该模型称为Artwork,我想在运行时为instante添加一个test_column列。 我只是在讨论,所以我添加了一个简单的链接来实现它,它当然是参数化的。

我设法通过迁移来实现:

def test_migration_create Artwork.add_column :test_column, :integer flash[:notice] = "Added Column test_column to artworks" redirect_to :action => 'index' end def test_migration_delete Artwork.remove_column :test_column flash[:notice] = "Removed column test_column from artworks" redirect_to :action => 'index' end 

它可以工作,从数据库中添加/删除列没有问题。 我现在正在使用active_scaffold,所以我在表单中获得test_column字段而不添加任何内容。 但是,当我提交创建或更新时,test_column不会更新并保持为空。 检查参数,我可以看到:

 Parameters: {"commit"=>"Update", "authenticity_token"=>"37Bo5pT2jeoXtyY1HgkEdIhglhz8iQL0i3XAx7vu9H4=", "id"=>"62", "record"=>{"number"=>"test_artwork", "author"=>"", "title"=>"Opera di Test", "test_column"=>"TEEST", "year"=>"", "description"=>""}} 

test_column参数正确传递。 那么为什么活跃的记录一直无视呢? 我试图重启服务器但没有成功。

我正在使用ruby 1.8.7,rails 2.3.5和mongrel与sqlite3数据库。

谢谢

在开发环境中,ActiveRecord模型会在每次请求时重新加载元数据。 但是,在生产环境中,元数据在启动时被缓存,因此在刷新元数据之前,您添加的任何列都不容易访问。

此外,更改表通常需要在重写数据时使用独占表锁,这可能会严重损害站点的性能。