添加在轨道一列模型在运行时(无附加表) [英] Adding a column to a model at runtime (without additional tables) in rails

查看:96
本文介绍了添加在轨道一列模型在运行时(无附加表)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想给我的web应用程序的管理员,以一些新的字段添加到模型的能力。该模型被称为艺术品,我想补充,为instante,一个test_column列在运行时。我只是有试,所以我说做一个简单的链接,这将是当然的参数。

I'm trying to give admins of my web application the ability to add some new fields to a model. The model is called Artwork and i would like to add, for instante, a test_column column at runtime. I'm just teting, so i added a simple link to do it, it will be of course parametric.

我设法通过迁移来做到这一点:

I managed to do it through migrations:

  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

它的工作原理,列被添加/没有问题,删除/从DATABSE。我使用active_scaffold的时刻,所以我得到的形式test_column领域无所添加。当我提出创建或更新,但是,test_column没有更新,保持空。检查的参数,我可以看到:

It works, the column gets added/ removed to/from the databse without issues. I'm using active_scaffold at the moment, so i get the test_column field in the form without adding anything. When i submit a create or an update, however, the test_column does not get updated and stay empty. Inspecting the parameters, i can see:

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

在test_column参数正确传递。那么,为什么活动记录保存忽略它?我试图重新启动服务器,也没有成功。

the test_column parameter is passed correctly. So why active record keeps ignoring it? I tried to restart the server too without success.

我使用的红宝石1.8.7,2.3.5导轨和杂种与sqlite3的数据库。

I'm using ruby 1.8.7, rails 2.3.5, and mongrel with an sqlite3 database.

感谢

推荐答案

在开发环境中,ActiveRecord模型在每一个请求重新加载它们的元数据。在生产环境中,但是,元数据缓存在启动时,所以你增加任何字段不会轻易访问,直到该元数据被刷新。

In the development environment, ActiveRecord models reload their metadata upon every request. In the production environment, however, metadata is cached at startup, so any columns you add will not be easily accessible until that metadata is refreshed.

此外,变更表通常需要在数据被改写排它表锁,这可能真的伤害了你的网站的性能。

Also, altering a table usually requires an exclusive table lock while the data is rewritten, which could really hurt your site's performance.

这篇关于添加在轨道一列模型在运行时(无附加表)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆