# Denormalizing in Rails

**URL:** <https://www.ruby-forum.com/t/denormalizing-in-rails/118866>\
**Category:** Rails\
**Created:** [October 31, 2007, 7:28pm UTC](https://www.ruby-forum.com/t/denormalizing-in-rails/118866 "2007-10-31T19:28:36Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![binkly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b5e925/32.png) [@binkly](https://www.ruby-forum.com/u/binkly)\
**Post date:** [October 31, 2007, 7:28pm UTC](https://www.ruby-forum.com/t/denormalizing-in-rails/118866/1 "2007-10-31T19:28:36Z")

</div>

Hello All,

I saw the following code in a slide deck and was hoping someone could  
help me out trying to understand it.

def self.up  
add\_column “users”, “friends\_ids”, :text  
end

class Friendship \< AR::Base

belongs to :user  
belongs\_to :friend

def add\_to\_denormalized\_friends  
user.friends\_ids \<\< friend.id  
user.friends\_ids.uniq!  
end

what i don’t get is, friends\_ids is text, so how does it get converted  
to an array so that uniq! can be called on it?

it seems like a neat trick and i’d really like to learn it if its real.

thanks,

binh

---

<div class="post-metadata">

**Author:** ![binkly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b5e925/32.png) [@binkly](https://www.ruby-forum.com/u/binkly)\
**Post date:** [October 31, 2007, 7:44pm UTC](https://www.ruby-forum.com/t/denormalizing-in-rails/118866/2 "2007-10-31T19:44:37Z")

</div>

On 10/31/07, Binh Ly [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> class Friendship \< AR::Base  
> to an array so that uniq! can be called on it?
> 
> it seems like a neat trick and i’d really like to learn it if its real.

You don’t show the definition of the User class, it’s possible that  
user\_ids is a composite attribute, which would be done using the  
composed\_of :user\_ids, … declaration.

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![binkly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b5e925/32.png) [@binkly](https://www.ruby-forum.com/u/binkly)\
**Post date:** [October 31, 2007, 7:49pm UTC](https://www.ruby-forum.com/t/denormalizing-in-rails/118866/3 "2007-10-31T19:49:53Z")

</div>

On 10/31/07, Rick DeNatale [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> You don’t show the definition of the User class, it’s possible that  
> user\_ids is a composite attribute, which would be done using the  
> composed\_of :user\_ids, … declaration.

Having just said that it’s probably not the case. More likely  
user\_ids is converted in before\_save and after\_initialize and/or  
after\_find callbacks.

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)
