# Virtual/Flexible attributes on model

**URL:** <https://www.ruby-forum.com/t/virtual-flexible-attributes-on-model/189587>\
**Category:** Rails\
**Created:** [June 3, 2010, 4:07pm UTC](https://www.ruby-forum.com/t/virtual-flexible-attributes-on-model/189587 "2010-06-03T16:07:51Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mariachi77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d2c977/32.png) [@mariachi77](https://www.ruby-forum.com/u/mariachi77)\
**Post date:** [June 3, 2010, 4:07pm UTC](https://www.ruby-forum.com/t/virtual-flexible-attributes-on-model/189587/1 "2010-06-03T16:07:51Z")

</div>

I’ve a requirement to put some user-managed attributes on a model.

Essentially, I want Entity-Attribute-Value functionality - the  
administrator has the ability to create “fields” on the model, and to  
specify their type (datetime, string, boolean, decimal, etc). Then in  
the regular views, those fields are show in addition to all the normal  
fields from the model’s table. This could work with a :has\_many  
relationship; with the parent model having an overloaded “method  
missing” call to look for any members of the associated attributes  
that match called-for methods.

I could roll-my-own, but thought I’d look at any existing EAV options  
first; but have found thin pickings!  
There’s a fork of “acts\_as\_eav\_model” on Github [1] which allows for  
flexible attributes on models; and to my mind, “all” I need to do to  
have this do what I want is have some form of admin system to populate  
the :meta\_columns hash with the user-declared options.

But can anyone suggest any other options that might be worth  
considering?

[1] [GitHub - dwg/acts\_as\_eav\_model: Entity-attribute-value model for ActiveRecord](http://github.com/dwg/acts_as_eav_model/)

---

<div class="post-metadata">

**Author:** ![mariachi77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d2c977/32.png) [@mariachi77](https://www.ruby-forum.com/u/mariachi77)\
**Post date:** [June 3, 2010, 4:16pm UTC](https://www.ruby-forum.com/t/virtual-flexible-attributes-on-model/189587/2 "2010-06-03T16:16:38Z")

</div>

On 3 June 2010 15:06, Michael P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I could roll-my-own, but thought I’d look at any existing EAV options  
> first; but have found thin pickings!  
> There’s a fork of “acts\_as\_eav\_model” on Github [1] which allows for  
> flexible attributes on models; and to my mind, “all” I need to do to  
> have this do what I want is have some form of admin system to populate  
> the :meta\_columns hash with the user-declared options.
> 
> But can anyone suggest any other options that might be worth considering?

I’d start with the simplest solution:

class UserAccount \< ActiveRecord::Base  
serialize :meta\_data, Hash  
end

Then work up from there as you need more functionality…

I’m sure others will recommend larger, more full featured solutions, but  
simple serialization may work for you/others reading this thread in the  
future.

Cheers,

Andy

---

<div class="post-metadata">

**Author:** ![mariachi77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d2c977/32.png) [@mariachi77](https://www.ruby-forum.com/u/mariachi77)\
**Post date:** [June 3, 2010, 4:20pm UTC](https://www.ruby-forum.com/t/virtual-flexible-attributes-on-model/189587/3 "2010-06-03T16:20:09Z")

</div>

agree,  
eav, is build based on data serialization

try it, if u will be required more, than simply switching to eav

Ivan N.  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![mariachi77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d2c977/32.png) [@mariachi77](https://www.ruby-forum.com/u/mariachi77)\
**Post date:** [June 3, 2010, 4:33pm UTC](https://www.ruby-forum.com/t/virtual-flexible-attributes-on-model/189587/4 "2010-06-03T16:33:02Z")

</div>

On 3 June 2010 15:15, Andy J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> class UserAccount \< ActiveRecord::Base  
> serialize :meta\_data, Hash  
> end

That would get it working to demo, but I _know_ that they’re going to  
want to search/filter on the attributes once they have their hands on  
them, so I’d prefer to start with a DB record per attribute that gets  
assigned.

---

<div class="post-metadata">

**Author:** ![mariachi77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d2c977/32.png) [@mariachi77](https://www.ruby-forum.com/u/mariachi77)\
**Post date:** [June 3, 2010, 4:38pm UTC](https://www.ruby-forum.com/t/virtual-flexible-attributes-on-model/189587/5 "2010-06-03T16:38:54Z")

</div>

> > class UserAccount \< ActiveRecord::Base  
> > serialize :meta\_data, Hash  
> > end
> 
> That would get it working to demo, but I _know_ that they’re going to  
> want to search/filter on the attributes once they have their hands on  
> them, so I’d prefer to start with a DB record per attribute that gets  
> assigned.

Fair enough.

I’ve not needed to do it yet, so I’ll bow out (just thought I’d post the  
simple solution in case someone out there finds this thread and didn’t  
know  
about it).

Good luck chap.

Cheers,

Andy

---

<div class="post-metadata">

**Author:** ![mariachi77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d2c977/32.png) [@mariachi77](https://www.ruby-forum.com/u/mariachi77)\
**Post date:** [June 4, 2010, 12:42am UTC](https://www.ruby-forum.com/t/virtual-flexible-attributes-on-model/189587/6 "2010-06-04T00:42:38Z")

</div>

Michael P. wrote:

> On 3 June 2010 15:15, Andy J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > class UserAccount \< ActiveRecord::Base  
> > serialize :meta\_data, Hash  
> > end
> 
> That would get it working to demo, but I _know_

Really? Or do you guess?

> that they’re going to  
> want to search/filter on the attributes once they have their hands on  
> them, so I’d prefer to start with a DB record per attribute that gets  
> assigned.

EAV is generally a bad idea. If serialize won’t work for you, then your  
best bet is probably a schemaless non-SQL database such as MongoDB or  
TokyoCabinet.

## Best,

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

---

<div class="post-metadata">

**Author:** ![mariachi77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d2c977/32.png) [@mariachi77](https://www.ruby-forum.com/u/mariachi77)\
**Post date:** [June 3, 2010, 4:37pm UTC](https://www.ruby-forum.com/t/virtual-flexible-attributes-on-model/189587/7 "2010-06-03T16:37:44Z")

</div>

may be watch to documented db? something like mongodb?

Ivan N.  
removed\_email\_address@domain.invalid
