# Adding dynamic field to a model

**URL:** <https://www.ruby-forum.com/t/adding-dynamic-field-to-a-model/92208>\
**Category:** Rails\
**Created:** [April 17, 2007, 4:11am UTC](https://www.ruby-forum.com/t/adding-dynamic-field-to-a-model/92208 "2007-04-17T04:11:53Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![damianrr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a587f6/32.png) [@damianrr](https://www.ruby-forum.com/u/damianrr)\
**Post date:** [April 17, 2007, 4:11am UTC](https://www.ruby-forum.com/t/adding-dynamic-field-to-a-model/92208/1 "2007-04-17T04:11:53Z")

</div>

Hi, here i describe my problem:

mi application must allow 2 basic issues:

first: the users may be able to create ‘people’ (this would be a model  
with a scaffold, and is not an issue, the problem comes with  
requierement #2)

second: the users should be able to add new fields to that model, for  
example: the model ‘person’ may have 2 fields, let say: ‘name’ and  
‘age’, the basic idea is to give the user the ability to create new  
fields like ‘sex’ and ‘occupation’ if he like to do so, i wouldn’t like  
to do ALTER TABLE to solve this … any idea will be most welcome …  
thanks

---

<div class="post-metadata">

**Author:** ![damianrr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a587f6/32.png) [@damianrr](https://www.ruby-forum.com/u/damianrr)\
**Post date:** [April 17, 2007, 11:49am UTC](https://www.ruby-forum.com/t/adding-dynamic-field-to-a-model/92208/2 "2007-04-17T11:49:14Z")

</div>

Typical of bulletin boards, I won’t (can’t) answer your question  
directly. What I’d suggest is this:

Create a new model, called something like personal\_properties. This  
might have columns property\_name and property\_value (and a foreign key,  
‘person\_id’)

Then a person can ‘has\_many :personal\_properties’

Your users can add as many properties as they like, call them what they  
like (sex, occupation etc) and give them any value they like  
(occupation=“dentist”, sex=“male”) etc.

I understand your motivation, but implementing this with no constraints  
on the values (based on the attribute) will leave your data pretty  
purposeless, unless you want to include this capability just to allow  
users to express themselves. They might like the ability to be  
sex=“cyborg”. Could be amusing.

But if you want to get all businessy, it doesn’t allow you to extract  
any interesting demographic information about your user base. Even  
knowing that sex=“male” and sex=“m” are probably the same thing is  
tricky.

Nat

---

<div class="post-metadata">

**Author:** ![damianrr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a587f6/32.png) [@damianrr](https://www.ruby-forum.com/u/damianrr)\
**Post date:** [April 17, 2007, 3:58pm UTC](https://www.ruby-forum.com/t/adding-dynamic-field-to-a-model/92208/3 "2007-04-17T15:58:28Z")

</div>

Damian Rr wrote:

> second: the users should be able to add new fields to that model, for  
> example: the model ‘person’ may have 2 fields, let say: ‘name’ and  
> ‘age’, the basic idea is to give the user the ability to create new  
> fields like ‘sex’ and ‘occupation’ if he like to do so, i wouldn’t like  
> to do ALTER TABLE to solve this … any idea will be most welcome …  
> thanks

I have a plugin that I developed that will help handle this problem. You  
can find out about it at:

[http://rubyforge.org/projects/flex-attributes/](http://rubyforge.org/projects/flex-attributes/)

It basically does what nathaniel described but also has some  
method\_missing magic to allow you to do:

joe.gender = ‘Male’;

Even though “gender” is not a real attribute on the peoples table. It is  
still what I consider “beta” software but it has been used successfully  
in two project. I just need to clean up the documentation and then I  
probably consider it out of “beta”.

Also as nathaniel said this will basically make your data not very  
accessible. Doing queries on these dynamic fields is difficult. But it  
is nice when you need to be able to just store and retrieve a bunch of  
data.

Eric

---

<div class="post-metadata">

**Author:** ![damianrr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a587f6/32.png) [@damianrr](https://www.ruby-forum.com/u/damianrr)\
**Post date:** [April 17, 2007, 10:28pm UTC](https://www.ruby-forum.com/t/adding-dynamic-field-to-a-model/92208/4 "2007-04-17T22:28:50Z")

</div>

thanks, i will give it a try …!

---

<div class="post-metadata">

**Author:** ![damianrr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a587f6/32.png) [@damianrr](https://www.ruby-forum.com/u/damianrr)\
**Post date:** [April 19, 2007, 5:16am UTC](https://www.ruby-forum.com/t/adding-dynamic-field-to-a-model/92208/5 "2007-04-19T05:16:15Z")

</div>

I have the same problem as Damian…

Eric A. wrote:

> I have a plugin that I developed that will help handle this problem. You  
> can find out about it at:
> 
> [http://rubyforge.org/projects/flex-attributes/](http://rubyforge.org/projects/flex-attributes/)

I can not download plugin.

> It basically does what nathaniel described but also has some  
> method\_missing magic to allow you to do:
> 
> joe.gender = ‘Male’;
> 
> Also as nathaniel said this will basically make your data not very  
> accessible. Doing queries on these dynamic fields is difficult.

TO avoid the difficult queries… can I use alter table? is it  
efficient?

But it

> is nice when you need to be able to just store and retrieve a bunch of  
> data.
> 
> Eric

Please place some ideas. Thx in advanced.

Milinda

---

<div class="post-metadata">

**Author:** ![damianrr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a587f6/32.png) [@damianrr](https://www.ruby-forum.com/u/damianrr)\
**Post date:** [April 19, 2007, 4:51pm UTC](https://www.ruby-forum.com/t/adding-dynamic-field-to-a-model/92208/6 "2007-04-19T16:51:40Z")

</div>

Eric Milinda wrote:

> I have the same problem as Damian…
> 
> Eric A. wrote:
> 
> > I have a plugin that I developed that will help handle this problem. You  
> > can find out about it at:
> > 
> > [http://rubyforge.org/projects/flex-attributes/](http://rubyforge.org/projects/flex-attributes/)
> 
> I can not download plugin.

It’s in subversion for checking out, rather than downloading.

> TO avoid the difficult queries… can I use alter table? is it  
> efficient?

I infer (possibly wrongly) from your question that don’t understand  
what’s meant by ‘alter table’ ? If that’s so, can I humbly suggest you  
stop worrying about efficiency and spend some time figuring out the  
basics.

If I read wrong, I apologise.

That was supposed to be a constructive criticism, I hope thats how it  
comes across 🙂

Alan

---

<div class="post-metadata">

**Author:** ![damianrr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a587f6/32.png) [@damianrr](https://www.ruby-forum.com/u/damianrr)\
**Post date:** [April 19, 2007, 3:20pm UTC](https://www.ruby-forum.com/t/adding-dynamic-field-to-a-model/92208/7 "2007-04-19T15:20:47Z")

</div>

Eric Milinda wrote:

> I can not download plugin.

It is in the SCM.

> TO avoid the difficult queries… can I use alter table? is it  
> efficient?

You could but that is now what that plugin does. The main problem with  
ALTER TABLE is that you would need to have the model reload the column  
information before any query. Also all objects will share the same set  
of possible columns while storing it in a thin table (as my plugin does)  
allows each object to determine what columns are available/used.

Eric
