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 Dominguez
Iplan Networks Datos Personales
rdominguez@iplan.com.ar rorra@rorra.com.ar
www.iplan.com.ar <http://www.iplan.com.ar/>
www.rorra.com.ar <http://www.rorra.com.ar/>
5031-6303 15-5695-6027
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://wrath.rubyonrails.org/pipermail/rails/attachments/20060404/562b146d/attachment.html
Rodrigo Dominguez wrote:> 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 arrayRecords 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.