Ive recently been contemplating how to tackle allowing for extra
“fields” to be added to a recordset. For example, a user has an email
address, but they may also want to put their birthday, height, weight,
etc. I don’t want to constrain what is possible in this situation. I
think what I’m describing is best documented in Philip G.spun’s
page here:
Now, I’m not entirely sure this is the best approach, I’m open to
suggestions on how to deal with the situation. What I would like to
know though is: Is this something that could be an extension of the
ActiveRecord class? Lets say its called ActiveSkinnyRecord or
something… is there a potential to make tables with this setup be
just as easily dealt with as ActiveRecord does? Id be willing to give
it a shot if it makes sense to others, and is doable. Please let me
know your thoughts.
Ive recently been contemplating how to tackle allowing for extra
“fields” to be added to a recordset. For example, a user has an email
address, but they may also want to put their birthday, height, weight,
etc. I don’t want to constrain what is possible in this situation.
You have at least three strategies available that you can use right
now without any futher additions to AR:
1.) Single Table Inheritance (admittedly not 100% what you’re looking
for, but it’s worth a mention anyway)
2.) has_many :attributes, :as => :attributable (polymorphic
relationship with a simple Attribute model)
3.) serialize :attributes (where User#attributes is a Hash that is
YAML’d into a TEXT column transparently.)
2.) has_many :attributes, :as => :attributable (polymorphic
relationship with a simple Attribute model)
Thanks Hendrik, I will check that out. I think the serialized way
while it may be convenient would probably be a bad deal for general
record finding/sorting… too bad cause that would be a real simple
solution.
Joe
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.