# Formatting data before ActiveRecord insertion

**URL:** <https://www.ruby-forum.com/t/formatting-data-before-activerecord-insertion/194480>\
**Category:** Rails\
**Created:** [September 6, 2010, 4:39pm UTC](https://www.ruby-forum.com/t/formatting-data-before-activerecord-insertion/194480 "2010-09-06T16:39:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![tomfakes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/87869e/32.png) [@tomfakes](https://www.ruby-forum.com/u/tomfakes)\
**Post date:** [September 6, 2010, 4:39pm UTC](https://www.ruby-forum.com/t/formatting-data-before-activerecord-insertion/194480/1 "2010-09-06T16:39:40Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![tomfakes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/87869e/32.png) [@tomfakes](https://www.ruby-forum.com/u/tomfakes)\
**Post date:** [September 6, 2010, 4:44pm UTC](https://www.ruby-forum.com/t/formatting-data-before-activerecord-insertion/194480/2 "2010-09-06T16:44:07Z")

</div>

On 5 September 2010 00:31, Tom F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> 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” }

Not answering the question, I know, but I would advise against doing  
it this way. Have a tracks table, User has\_many tracks, Track  
belongs\_to user. Then for a user his tracks are  
current\_user.tracks  
and for each track the artist and name are track.artist and track.name.  
You will not have to write all the code you are struggling with, Rails  
will do it for you. The less code you have to write the better.

Colin

---

<div class="post-metadata">

**Author:** ![tomfakes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/87869e/32.png) [@tomfakes](https://www.ruby-forum.com/u/tomfakes)\
**Post date:** [September 6, 2010, 4:39pm UTC](https://www.ruby-forum.com/t/formatting-data-before-activerecord-insertion/194480/3 "2010-09-06T16:39:42Z")

</div>

You can manipulate data before saving it

class User  
before\_save :do\_something

def do\_something  
// process music\_tracks here  
end  
end

PS: I do not know the purpose of your project, but in general, having  
a table of songs referred by id seems a better option to me.

Song 1 =\> { band =\> “Kiss”, “title” =\> “Strutter” }  
User 1 =\> [1, 1, 1]  
User 2 =\> [1, 1, 1]  
User 3 =\> [1, 1, 1]

In your case, you will get multiple copies of bloated data spread all  
over DB if many users listen to the same song over and over again.

User 1 =\> { 1 =\> “Kiss: Strutter”,  
2 =\> “Kiss: Strutter”,  
3 =\> “Kiss: Strutter” }  
User 2 =\> { 1 =\> “Kiss: Strutter”,  
2 =\> “Kiss: Strutter”,  
3 =\> “Kiss: Strutter” }  
User 3 =\> { 1 =\> “Kiss: Strutter”,  
2 =\> “Kiss: Strutter”,  
3 =\> “Kiss: Strutter” }
