Forum: Ruby on Rails Objects in Arrays? Allowed, right?

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.
39025a7f572d6fe464e1fe6be399ca3b?d=identicon&s=25 Mike Dershowitz (mdersh53)
on 2007-02-12 15:28
Hi:

I'm trying to manually append objects to an instance variable that is
just an array of objects.  Here's my controller code:

@tasks = Task.find(:all, :conditions => "entity_id =
#{session[:user].id}")
for t in @tasks
  if Subtasks.find(:first, :conditions => "child_id = #{t.id}") != nil
    @mtasks << t
  end
end

However, when I execute, I get an error:

You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.<<

Am i incorrectly referencing the object "t"?  I haven't previously
declared or used "mtasks".

Any help you could provide would be greatly appreciated.

Thanks!

Mike
77f306388bb6ae00ac0b0401e27cdc99?d=identicon&s=25 Chad Fowler (Guest)
on 2007-02-12 15:44
(Received via mailing list)
On 2/12/07, Mike Dershowitz <rails-mailing-list@andreas-s.net> wrote:
>     @mtasks << t
> declared or used "mtasks".
>
>

Looks like a typo:  "@tasks" vs. "@mtasks".

Chad
D7c511ce5025d37b8c6bd9134e0f2bd9?d=identicon&s=25 Thorsten L (Guest)
on 2007-02-12 15:57
(Received via mailing list)
You didn't initialize the @mtasks as an array. Try this:

@tasks = Task.find(:all, :conditions => "entity_id =
#{session[:user].id}")
@mtasks = Array.new()
for t in @tasks
  if Subtasks.find(:first, :conditions => "child_id = #{t.id}") !=
nil
    @mtasks << t
  end
end




On 12 Feb., 15:28, Mike Dershowitz <rails-mailing-l...@andreas-s.net>
This topic is locked and can not be replied to.