# Adding columns to DB on the fly

**URL:** <https://www.ruby-forum.com/t/adding-columns-to-db-on-the-fly/177001>\
**Category:** Rails\
**Created:** [October 9, 2009, 3:44pm UTC](https://www.ruby-forum.com/t/adding-columns-to-db-on-the-fly/177001 "2009-10-09T15:44:28Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![kerdany](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/71e660/32.png) [@kerdany](https://www.ruby-forum.com/u/kerdany)\
**Post date:** [October 9, 2009, 3:44pm UTC](https://www.ruby-forum.com/t/adding-columns-to-db-on-the-fly/177001/1 "2009-10-09T15:44:28Z")

</div>

Hi,  
I’m doing a multilanguage application which kind-of abuses the concept  
of dynamic models of Rails.

I allow the application administrator to create a new language (theres  
a Language model), and each time a new language is created, I  
automatically add a new column that corresponds to an existing column  
in the database.

for example, after creating a “Spanish” language, the code searches  
the model “Products” for columns that end with \_lang\_en (where en is  
the default language), and then creates an equivalent column with the  
same type (type, constrains, and indices), which is suffixed by  
\_lang\_es.  
(then i’d dynamically show the new language text fields on the views).

Things are working perfectly in creating the new columns, however,  
some weird errors happen when trying to display pages that load data  
from the internationalized models, for example, “undefined method  
`each\_lang\_column’ for #Currency:0xb67fc220”, which is a method  
defined in a parent class that inherits from ActiveRecord::Base, while  
the code works perfectly when I restart the application.

My question is, is there a way to reloads all application classes/  
restarts the application from the code? (I’m aware of the .reload!  
method, but this is not what I need, it works on the object level, and  
loads the data, not the definitions).

---

<div class="post-metadata">

**Author:** ![kerdany](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/71e660/32.png) [@kerdany](https://www.ruby-forum.com/u/kerdany)\
**Post date:** [October 9, 2009, 5:19pm UTC](https://www.ruby-forum.com/t/adding-columns-to-db-on-the-fly/177001/2 "2009-10-09T17:19:50Z")

</div>

kerdany wrote:

> Hi,  
> I’m doing a multilanguage application which kind-of abuses the concept  
> of dynamic models of Rails.
> 
> I allow the application administrator to create a new language (theres  
> a Language model), and each time a new language is created, I  
> automatically add a new column that corresponds to an existing column  
> in the database.
> 
> for example, after creating a “Spanish” language, the code searches  
> the model “Products” for columns that end with \_lang\_en (where en is  
> the default language), and then creates an equivalent column with the  
> same type (type, constrains, and indices), which is suffixed by  
> \_lang\_es.  
> (then i’d dynamically show the new language text fields on the views).

This is horrible database design. I would recommend moving the  
multilingual descriptions into a separate table:

Product has\_many :descriptions

Description belongs\_to :product, belongs\_to :language

then:

products:  
id: 1  
price: 0.99

descriptions:  
id: 1  
product\_id: 1  
language\_id: en # using ISO codes rather than numbers for this example  
name: Pencil

id: 2  
product\_id: 1  
language\_id: es  
name: LÃ¡piz

and so on.

Does that make sense? If not, I’ll explain further. Clean, relational,  
DRY, no reloading necessary.

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid
