ActiveRecord object


#1

I have an ActiveRecord class

class User < ActiveRecord::Base

end

and I want to define an attribute, in example “links”, links will be an
array.

class User < ActiveRecord::Base

attr :links

end

and now, I want links to be an array, so I do

class User < ActiveRecord::base

attr:links

def initialize(attributes)

super(attributes)

@links = Array.new

yield self if block_given?

end

end

but then I do

users = User.find(:all)

user = users[0]

user.links.find {|x| x == “foo”}

and it gives me an error because it says that user.links is null

What is my mistake? I just need the attribute links to be an array

Rodrigo D.

Iplan Networks Datos Personales

removed_email_address@domain.invalid removed_email_address@domain.invalid

www.iplan.com.ar http://www.iplan.com.ar/
www.rorra.com.ar http://www.rorra.com.ar/

5031-6303 15-5695-6027


#2

Rodrigo D. wrote:

but then I do

users = User.find(:all)
user = users[0]
user.links.find {|x| x == ?foo?}

and it gives me an error because it says that user.links is null

What is my mistake? I just need the attribute links to be an array

Records returned by find are not instantiated using new, which
calls initialize, but by allocate, which does not. Try overriding
allocate and setting links in there.


We develop, watch us RoR, in numbers too big to ignore.