Adding a column to the model at run time (without additional tables) in rails

I am trying to give administrators of my web application the ability to add some new fields to the model. The model is called Artwork, and I would like to add a test_column column at run time for the instance. I'm just recording, so I added a simple link to do this, it will, of course, be parametric.

I managed to do this 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 

This works, the column is added / removed to / from the database without problems. I am using active_scaffold at the moment, so I get the test_column field in the form without adding anything. However, when I submit the creation or update, test_column is not updated and remains empty. Checking the parameters, I 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 does the active record continue to ignore it? I also tried restarting the server without success.

I am using ruby ​​1.8.7, rails 2.3.5 and mongrel with sqlite3 database.

thanks

+4
source share
1 answer

In the development environment, ActiveRecord models reload their metadata for each request. However, in a production environment, metadata is cached at startup, so any added columns will not be available until these metadata are updated.

In addition, changing a table usually requires an exclusive table lock when overwriting data, which can severely damage the performance of your site.

+1
source

Source: https://habr.com/ru/post/1308538/


All Articles