# Nil object error - not under console

**URL:** <https://www.ruby-forum.com/t/nil-object-error-not-under-console/141849>\
**Category:** Rails\
**Created:** [July 1, 2008, 3:24am UTC](https://www.ruby-forum.com/t/nil-object-error-not-under-console/141849 "2008-07-01T03:24:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jfoxny](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a6a055/32.png) [@jfoxny](https://www.ruby-forum.com/u/jfoxny)\
**Post date:** [July 1, 2008, 3:24am UTC](https://www.ruby-forum.com/t/nil-object-error-not-under-console/141849/1 "2008-07-01T03:24:35Z")

</div>

Hi there,

I’m running my app using rails 2.0.2.

When running the create method in my controller, I get the following  
error:

* * *

You have a nil object when you didn’t expect it!  
You might have expected an instance of ActiveRecord::Base.  
The error occurred while evaluating nil.[]

app/controllers/routes\_controller.rb:39:in `create’

Parameters:

{“commit”=\>“Create”,  
“route”=\>{“name”=\>“testroute”},  
“user\_ids”=\>[“1”,  
“4”],  
“heading\_id”=\>“0”

* * *

Here’s line 39 in the controller:

@route.users = User.find(@params[:user\_ids]) if @params[:user\_ids]

I don’t understand what’s going on here. The following works when I run  
it in the console:

User.find([1,4])

I’m at a loss as to why this doesn’t work.

Can anyone help me here?

Thanks!

---

<div class="post-metadata">

**Author:** ![jfoxny](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a6a055/32.png) [@jfoxny](https://www.ruby-forum.com/u/jfoxny)\
**Post date:** [July 1, 2008, 3:48am UTC](https://www.ruby-forum.com/t/nil-object-error-not-under-console/141849/2 "2008-07-01T03:48:04Z")

</div>

On Jun 30, 2008, at 9:24 PM, Jason F. wrote:

> You might have expected an instance of ActiveRecord::Base.  
> “heading\_id”=\>“0”
> 
> * * *
> 
> Here’s line 39 in the controller:
> 
> @route.users = User.find(@params[:user\_ids]) if @params[:user\_ids]

Should be @route.users = User.find(params[:user\_ids]) if  
params[:user\_ids]

The current code is trying to use an instance variable name params

Best.  
Mike

---

<div class="post-metadata">

**Author:** ![jfoxny](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a6a055/32.png) [@jfoxny](https://www.ruby-forum.com/u/jfoxny)\
**Post date:** [July 1, 2008, 3:51am UTC](https://www.ruby-forum.com/t/nil-object-error-not-under-console/141849/3 "2008-07-01T03:51:23Z")

</div>

> Should be @route.users = User.find(params[:user\_ids]) if  
> params[:user\_ids]

Well, that fixed it. The previous code I had did work, maybe when I  
upgraded to rails 2.0 it stopped? This method doesn’t get run much and  
could have gone unnoticed.

Thanks so much for your help.

Jason

---

<div class="post-metadata">

**Author:** ![jfoxny](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a6a055/32.png) [@jfoxny](https://www.ruby-forum.com/u/jfoxny)\
**Post date:** [July 1, 2008, 4:02am UTC](https://www.ruby-forum.com/t/nil-object-error-not-under-console/141849/4 "2008-07-01T04:02:41Z")

</div>

You’re welcome Jason. You can check out this post for more detail on  
@params vs. params

> **[Use params, not @params](https://rubyonrails.org/2006/4/25/use-params-not-params)**
>
> I still frequently see people in the #rubyonrails channel using @params in their code. For a while now @params has been deprecated in favor of simply params. For those who just skim these blog posts:Use params, not @paramsWhy? When you use the params...

Best.  
Mike
