Topic: Add custom fields to model

Hello,

I want to let my user append custom fields to some of my models. Custom fields should be related to one object only (example : custom "Price" field added to item number 5 or custom "Size" field added to item number 12).

Is there an active_record plugin for that ? If there's no plugin, can you give some advice on how to implement this ?

Regards,

Osaris

Re: Add custom fields to model

osaris wrote:

Hello,

I want to let my user append custom fields to some of my models. Custom fields should be related to one object only (example : custom "Price" field added to item number 5 or custom "Size" field added to item number 12).

Is there an active_record plugin for that ? If there's no plugin, can you give some advice on how to implement this ?

Regards,

Osaris

I had a similar problem to this, where one of my models has user-customisable fields.  I'm really really lairy about dynamic database table changes, so didn't want to add or remove any columns from the table during runtime.  It also doesn't make sense - what if you have a million users, each with lots of fields that no-one else uses?  You have a table with millions of columns!  Crazy.

One option is to section this out into a different table with two columns, name & value, and link it with the first table somehow. That sounds kind of awkward though.  What i did instead is create a field called 'data', which serializes a hash. 

#in model
serialize :data, Hash

Then, this hash can be built up any way you want - one user could have {:name => "dave smith", :address => "26 madeup street, london"} and another user could have {:time => 10, :email => "fake@fakezzz.com"}. 

So, the table has all the columns that are required for the stuff that never changes, and the stuff that varies on a per-user basis is just shoved into a hash and serialized into the data column.  Then you can do stuff like, in a view that draws a table:

<% user.data.each do |k,v| %>
  <td><%= v %></td>
<% end %>

If you want, you could even add a method_missing to your model, that dynamically fills in get and set methods to wrap the data hash, so you could write

user.some_field = "foo"

and that gets converted to

user.data[:some_field] = "foo"

and similarly for the get method.

Last edited by Max Williams (2008-09-15 10:20:27)

###########################################
#If i've helped you then please recommend me at Working With Rails:
#http://www.workingwithrails.com/person/ … i-williams

Re: Add custom fields to model

Thanks a lot for this precise reply. In fact what I want to do is a little bit more tricky :

model "cars" => each car could have user fields (car A : option list for color, car B : text field ...). Then when user buy a car A, he must fill the corresponding generated form.

It's a little bit strange that no plugin try to manage custom fields for a model.

Re: Add custom fields to model

I think the issue adressed in this post can be useful for other people so I try to complete my reflexion with more informations.

There's multiple issues with custom fields :

* how to describe the form field in the database so a form can be generated for these custom fields ?
* how to manage validation (description and validation itself) ? Is it possible to create dynamically a sort of ActiveForm model to validate data ?
* how to let user edit again it's data (what is the best way to store user data of the custom fields in order to populate an edit form easily ?)

I must admit that I've still no answers to these questions.

Re: Add custom fields to model

Hi all,

My requirement is not exactly same as the one discussed here. Mine is like this:
I have a list model and a contact model.
Each list can have many contacts.
And for each list, user can specify what fields he wants to display.
For list1, user may have defined name,age,email.
So when a contact is added for this list, that contact should also have these fields.
For list2, user may have defined name,location,email.
When adding contact to list2, that particular contact should have these fields.

How can I implement this?

Initially I thought of has_magic_column_fork plugin (http://github.com/LeonB/has_magic_columns_fork )

So after creating a list, when the user defines fields for that list, i will create magic_columns for that list as given below:
@list.magic_columns << MagicColumn.create(:name => "age", :datatype => "integer")

Later when i add contact to this list, i'll see what are tyhe custom fields for this list and will add custom fields for these contacts also as given above.
But my problem is this :

Suppose I have the custom field 'age' for list1.
Then when i add contact1 to this list, i'll add the custom field 'age' for this contact.
Then I am creating another list list2 with custom field 'age' again.
And I am adding the same contact 'contact1' to the second list also. So this will again add the custom field 'age' to contact1?
Will this create issue when i retrieve values for this field? How can I proceed? Is there any other better approach??
Any help would be much appreciated.

Thanks,

Anu