# Adding custom fields dynamically to a model

**URL:** <https://www.ruby-forum.com/t/adding-custom-fields-dynamically-to-a-model/187712>\
**Category:** Rails\
**Created:** [April 28, 2010, 6:22am UTC](https://www.ruby-forum.com/t/adding-custom-fields-dynamically-to-a-model/187712 "2010-04-28T06:22:42Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![pankaj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a9adbd/32.png) [@pankaj](https://www.ruby-forum.com/u/pankaj)\
**Post date:** [April 28, 2010, 6:22am UTC](https://www.ruby-forum.com/t/adding-custom-fields-dynamically-to-a-model/187712/1 "2010-04-28T06:22:42Z")

</div>

I have a model called List which has many records

class List

has\_many :records

end

class Record

end

The table Record has 2 permanent fields: name, email. Besides these 2  
fields, for each List a Record can have ‘n’ custom fields.

for example: for list1 I add address(text), dob(date) as custom  
fields. then while adding records to list one, each record can have  
values for address and dob.

Is there any activerecord plugin which provides this type of  
functionality. Else could you share your thoughts on how to model  
this.

Thanks in advance, Pankaj

---

<div class="post-metadata">

**Author:** ![pankaj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a9adbd/32.png) [@pankaj](https://www.ruby-forum.com/u/pankaj)\
**Post date:** [April 28, 2010, 11:21am UTC](https://www.ruby-forum.com/t/adding-custom-fields-dynamically-to-a-model/187712/2 "2010-04-28T11:21:49Z")

</div>

Are you talking about a dynamic view here?  
I.E the table ‘records’ has 10 fields, but for this particular list  
only 3 of them are going to be used?

BTW, if your table is called “records” it will cause problems for  
ActiveRecords finds. I suggest you rename it to something else.

---

<div class="post-metadata">

**Author:** ![pankaj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a9adbd/32.png) [@pankaj](https://www.ruby-forum.com/u/pankaj)\
**Post date:** [April 28, 2010, 3:18pm UTC](https://www.ruby-forum.com/t/adding-custom-fields-dynamically-to-a-model/187712/3 "2010-04-28T15:18:14Z")

</div>

pankaj wrote:

> I have a model called List which has many records
> 
> class List
> 
> has\_many :records
> 
> end
> 
> class Record
> 
> end
> 
> The table Record has 2 permanent fields: name, email. Besides these 2  
> fields, for each List a Record can have ‘n’ custom fields.
> 
> for example: for list1 I add address(text), dob(date) as custom  
> fields. then while adding records to list one, each record can have  
> values for address and dob.
> 
> Is there any activerecord plugin which provides this type of  
> functionality. Else could you share your thoughts on how to model  
> this.

If there’s really no common schema beyond name and e-mail, then you have  
several options to play with:

- Use a serialized hash for the “extra” fields.
- Put the “extra” fields in some other table and use associations.
- Use a schemaless database such as MongoDB.

This is not something that SQL databases do easily.

> Thanks in advance, Pankaj

## Best,

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

---

<div class="post-metadata">

**Author:** ![pankaj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a9adbd/32.png) [@pankaj](https://www.ruby-forum.com/u/pankaj)\
**Post date:** [April 28, 2010, 4:58pm UTC](https://www.ruby-forum.com/t/adding-custom-fields-dynamically-to-a-model/187712/4 "2010-04-28T16:58:09Z")

</div>

Marnen Laibow-Koser wrote:

> - Put the “extra” fields in some other table and use associations.

You _could_ implement a simple “key - key value” model, and tie that  
back to the record and list since it seems that you’ll have different  
custom fields per list.

Perhaps the “list” model has an associated list of custom field  
definitions (name, type, size, etc?)

Each “record” for the list finds out what its list’s custom fields are,  
then maintains values for those in its associated table of ‘custom field  
values’

Or something like that…
