Ruby on Rails Saturday, September 4, 2010

Hello,


Sorry if this is a noob question.

I'm trying to store a hash of values (a user's music collection) into a single database record without having to have a separate row in the db for each music track the user has in his collection.

e.g. User 9
{ 12319 => "Artist X: Song Name 1", 
  12198 => "Artist X: Song Name 2",
  19180 => "Artist Y: Song Name 3",
  92810 => "Artist Z: Song Name 4" }

I'm going serialize this hash with serialize: and store it in a text field.

I'd like the create, update_attributes, save, etc. methods that could potentially write to this field to create this hash structure (if none exists), add to the hash structure, or remove an entity from this structure automatically without someone using my model having to manually getting the record, read the hash and adding/deleting from the hash.

I've thought about the following as options:

1) attr_protected :music_tracks
and write a custom accessor to manipulate the hash.

2) overriding the create, update_attributes, save etc. methods of ActiveRecord to manipulate the hash.

Am I going about this wrong, or any suggestions on the 2 options?

Thanks,
Tom


--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

No comments:

Post a Comment