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?
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.
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.
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).
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.