I’m setting up my users’ profiles as a set of key/value pairs…
where each key/value pair is a row in an associated table, like so:
user_id
profile_key
value
The profile_keys themselves are associations to a “profile_keys”
table (yea, way too organized, I know.
For some syntax sugar, I’ve set up some methods on the “profile”
has_many association on the user:
def [](k)
return fetch(socialize(k))
end
def []=(k, v)
kk = socialize k
user = @owner
if entry = fetch_object(kk)
entry.value = v
else
entry = UserProfile.new(
:profile_key => kk, :value => v, :user => user
)
end
entry.save
return entry
end
def socialize(key)
if key.class == Fixnum
key = UserProfileKey.find key
elsif key.class == String or key.class == Symbol
key = UserProfileKey.find_by_name key.to_s
end
return key
end
def fetch_object(profile_key)
result = self.collect { |x| x if x.profile_key == profile_key }
result.compact!
if v = result.first
return v
else
return nil
end
end
def fetch(profile_key)
if v = fetch_object(profile_key)
return v.value
else
return nil
end
end
Before I go too much further down this path… I’m sure somebody
else out there has set up an associated key/value pair table… is there
some published prior art on this? A plugin perhaps? (If not, the end
result
is probably going to be me writing one…
Thanks,
Tyler