Adding a column to a model at runtime (without additional tables) in rails
Posted
by Marek
on Stack Overflow
See other posts from Stack Overflow
or by Marek
Published on 2010-05-01T11:41:12Z
Indexed on
2010/05/01
11:47 UTC
Read the original article
Hit count: 193
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
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"=>""}}
the test_column parameter is passed correctly. So why active record keeps ignoring it? I tried to restart the server too without success.
I'm using ruby 1.8.7, rails 2.3.5, and mongrel with an sqlite3 database.
Thanks
© Stack Overflow or respective owner