Denormalizing in Rails

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

On 10/31/07, Binh Ly [email protected] 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/

On 10/31/07, Rick DeNatale [email protected] 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/