Adding dynamic field to a model

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

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

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/

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

thanks, i will give it a try …!

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/

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

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/

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 :slight_smile:

Alan

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