Forum: Ruby on Rails ActiveRecord object

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
B192011964906cffb106241f99cb4c3f?d=identicon&s=25 Rodrigo Dominguez (rdominguez)
on 2006-04-04 20:36
(Received via mailing list)
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
C64e63b70be7dfed8b0742540b8b27e5?d=identicon&s=25 Mark Reginald James (Guest)
on 2006-04-05 07:27
(Received via mailing list)
Rodrigo Dominguez 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.
This topic is locked and can not be replied to.