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.
Rodrigo D. (Guest)
on 2006-04-04 22: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 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
Mark Reginald J. (Guest)
on 2006-04-05 09:27
(Received via mailing list)
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.
This topic is locked and can not be replied to.