# Nill object error

**URL:** <https://www.ruby-forum.com/t/nill-object-error/188903>\
**Category:** Rails\
**Created:** [May 20, 2010, 8:56am UTC](https://www.ruby-forum.com/t/nill-object-error/188903 "2010-05-20T08:56:11Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![drk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b5ac83/32.png) [@drk](https://www.ruby-forum.com/u/drk)\
**Post date:** [May 20, 2010, 8:56am UTC](https://www.ruby-forum.com/t/nill-object-error/188903/1 "2010-05-20T08:56:11Z")

</div>

def grouplist

@currentgroupid=Group.find\_by\_group\_name(params[:group\_name])  
@currentgroupfriends=GroupFriend.find\_all\_by\_user\_id\_and\_group\_id(current\_user.id,@currentgroupid)

format.html {redirect\_to :back}

end

Please check my controller and help me in solving my problem.

I need to get groupid from the group\_name from groups table and  
The list of friendid s for in the group created by the user which are  
available in the groupfriends table.  
My requirement  
Ex : select friends from groupfriends where userid=current\_user.id and  
groupid=currentgroupid

Please help me in writing the above query in the controller in rails.

Thanks.

---

<div class="post-metadata">

**Author:** ![drk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b5ac83/32.png) [@drk](https://www.ruby-forum.com/u/drk)\
**Post date:** [May 20, 2010, 3:33pm UTC](https://www.ruby-forum.com/t/nill-object-error/188903/2 "2010-05-20T15:33:11Z")

</div>

Ravi D. wrote:

> def grouplist
> 
> @currentgroupid=Group.find\_by\_group\_name(params[:group\_name])  
> @currentgroupfriends=GroupFriend.find\_all\_by\_user\_id\_and\_group\_id(current\_user.id,@currentgroupid)
> 
> format.html {redirect\_to :back}
> 
> end

What does the log tell you is nil? current\_user or @currentgroupid?

a. You’re assuming that @currentgroupid will always have a value, which  
may be okay depending on the source of params[:group\_name], which we  
don’t know.

b. What is populating current\_user? Is that the source of the nil  
error?

---

<div class="post-metadata">

**Author:** ![drk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b5ac83/32.png) [@drk](https://www.ruby-forum.com/u/drk)\
**Post date:** [May 20, 2010, 5:35pm UTC](https://www.ruby-forum.com/t/nill-object-error/188903/3 "2010-05-20T17:35:45Z")

</div>

Ar Chron wrote:

> Ravi D. wrote:
> 
> > def grouplist
> > 
> > @currentgroupid=Group.find\_by\_group\_name(params[:group\_name])  
> > @currentgroupfriends=GroupFriend.find\_all\_by\_user\_id\_and\_group\_id(current\_user.id,@currentgroupid)
> > 
> > format.html {redirect\_to :back}
> > 
> > end
> 
> What does the log tell you is nil? current\_user or @currentgroupid?
> 
> a. You’re assuming that @currentgroupid will always have a value, which  
> may be okay depending on the source of params[:group\_name], which we  
> don’t know.
> 
> b. What is populating current\_user? Is that the source of the nil  
> error?

Thanks for your reply

def showfriendslist

@currentgroupid=Group.find\_by\_group\_name(params[:group\_name]).id  
@friendsingroup=GroupFriend.find\_all\_by\_user\_id\_and\_group\_id(current\_user.id,  
@currentgroupid)

respond\_to do |format|  
format.html {render :partial =\> “friendlist” }  
format.xml

end

This is my controller. My current\_user is the current logged in user.  
I can get the groupid value into @currentgroupid.

When I tried in console

> > @currentgroupid=Group.find\_by\_group\_name(‘testinggroup’)  
> > =\> #\<Group id: 23, created\_at: “2010-05-17 14:00:13”, updated\_at:  
> > “2010-05-17 14:00:13”, user\_id: 9, group\_name: “testinggroup”\>
> 
> > @currentgroupid.id  
> > =\> 23
> 
> > @friendsingroup=GroupFriend.find\_all\_by\_user\_id\_and\_group\_id(‘9’,@currentgroupid.id )  
> > =\> [#\<GroupFriend id: 15, created\_at: “2010-05-19 04:36:21”, updated\_at:  
> > “2010-05-19 04:36:21”, group\_id: 23, friend\_id: 4, user\_id: 9\>]
> 
> > @friendsingroup.id  
> > (irb):42: warning: Object#id will be deprecated; use Object#object\_id  
> > =\> -618448038
> 
> > @friendsingroup.group\_id  
> > NoMethodError: undefined method `group\_id’ for #Array:0xb64676b4  
> > from (irb):43

In view I get the following error

RuntimeError in UsersController#showfriendslist

Called id for nil, which would mistakenly be 4 – if you really wanted  
the id of nil, use object\_id

Please help… Im new to Ruby on Rails.

Thank you.

end

---

<div class="post-metadata">

**Author:** ![drk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b5ac83/32.png) [@drk](https://www.ruby-forum.com/u/drk)\
**Post date:** [May 20, 2010, 6:00pm UTC](https://www.ruby-forum.com/t/nill-object-error/188903/4 "2010-05-20T18:00:13Z")

</div>

On 20 May 2010 16:35, Ravi D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > >
> 
> format.xml  
> “2010-05-17 14:00:13”, user\_id: 9, group\_name: “testinggroup”\>  
> from (irb):43  
> That is because @friendsingroup is an array because you have used  
> find\_all\_by\_… The clue is in the error message. It says that class  
> Array has not got a method group\_id

> In view I get the following error
> 
> RuntimeError in UsersController#showfriendslist
> 
> Called id for nil, which would mistakenly be 4 – if you really wanted  
> the id of nil, use object\_id

That means that you have used something.id where id is nil. You still  
have not told us which line in the view is giving the error. Look at  
the error message and see which line it is, then look to see what id  
you are using on that line and most likely you will find the nil  
object. Then you just have to work out why it is nil. In order to  
avoid your view crashing you could test it for nil and put a message  
in the view instead of allowing it to crash.

I also suggest also that you have a look at the Rails Guide on  
debugging. It will show you ways of breaking in to your code to  
inspect the variables. I use ruby-debug when I have this sort of  
problem.

Colin

---

<div class="post-metadata">

**Author:** ![drk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b5ac83/32.png) [@drk](https://www.ruby-forum.com/u/drk)\
**Post date:** [May 20, 2010, 6:19pm UTC](https://www.ruby-forum.com/t/nill-object-error/188903/5 "2010-05-20T18:19:21Z")

</div>

Colin L. wrote:

> On 20 May 2010 16:35, Ravi D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > > >
> > 
> > ï¿½format.xml  
> > “2010-05-17 14:00:13”, user\_id: 9, group\_name: “testinggroup”\>  
> > ï¿½from (irb):43  
> > That is because @friendsingroup is an array because you have used  
> > find\_all\_by\_… The clue is in the error message. It says that class  
> > Array has not got a method group\_id
> 
> > In view I get the following error
> > 
> > ï¿½RuntimeError in UsersController#showfriendslist
> > 
> > Called id for nil, which would mistakenly be 4 – if you really wanted  
> > the id of nil, use object\_id
> 
> That means that you have used something.id where id is nil. You still  
> have not told us which line in the view is giving the error. Look at  
> the error message and see which line it is, then look to see what id  
> you are using on that line and most likely you will find the nil  
> object. Then you just have to work out why it is nil. In order to  
> avoid your view crashing you could test it for nil and put a message  
> in the view instead of allowing it to crash.
> 
> I also suggest also that you have a look at the Rails Guide on  
> debugging. It will show you ways of breaking in to your code to  
> inspect the variables. I use ruby-debug when I have this sort of  
> problem.
> 
> Colin

Thanks Colin for the reply.

When debugging i find currentgroupid is nill.

How can I solve this. Possible solutions to pass values form partial to  
controller.  
\<% @groups.each do |g| %\>

##### \<%= link\_to\_remote g.group\_name, :update =\> "abc", :url =\> { :controller =\> "users" , :action =\> "showfriendslist" } %\>

\<%= hidden\_field\_tag :group\_name %\>

\<% end %\>

I can see my groupname displaying in my page, but when I click on my  
groupname link, Im not able to pass groupname to my controller.

My controller:

def showfriendslist

@currentgroupid=Group.find\_by\_group\_name(params[:group\_name]).id

#@friendsingroup=GroupFriend.find\_all\_by\_user\_id\_and\_group\_id(current\_user.id,@currentgroupid)

respond\_to do |format|  
format.html {render :partial =\> “friendlist” }

format.xml  
end

end

Please help. Thank you.

---

<div class="post-metadata">

**Author:** ![drk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b5ac83/32.png) [@drk](https://www.ruby-forum.com/u/drk)\
**Post date:** [May 20, 2010, 9:58pm UTC](https://www.ruby-forum.com/t/nill-object-error/188903/6 "2010-05-20T21:58:29Z")

</div>

On 20 May 2010 17:19, Ravi D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> “users” , :action =\> “showfriendslist” } %\>
> 
> \<%= hidden\_field\_tag :group\_name %\>
> 
> \<% end %\>
> 
> I can see my groupname displaying in my page, but when I click on my  
> groupname link, Im not able to pass groupname to my controller.

Using a hidden field does not work as you are not in a form. That is  
how to do it if you were submitting from a form. Instead just include  
the name in the url - something like  
\<%= link\_to\_remote g.group\_name, :update =\> “abc”, :url =\> {  
:controller =\> “users” , :action =\> “showfriendslist”, :group\_name =\>  
g.group\_name } %\>

Look at the html generated by this to check that it is being included  
correctly.

Colin

---

<div class="post-metadata">

**Author:** ![drk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b5ac83/32.png) [@drk](https://www.ruby-forum.com/u/drk)\
**Post date:** [May 21, 2010, 12:37am UTC](https://www.ruby-forum.com/t/nill-object-error/188903/7 "2010-05-21T00:37:14Z")

</div>

> > > I can see my groupname displaying in my page, but when I click on my  
> > > groupname link, Im not able to pass ï¿½groupname to my controller.
> > 
> > Using a hidden field does not work as you are not in a form. That is  
> > how to do it if you were submitting from a form. Instead just include  
> > the name in the url - something like  
> > \<%= link\_to\_remote g.group\_name, :update =\> “abc”, :url =\> {  
> > :controller =\> “users” , :action =\> “showfriendslist”, :group\_name =\>  
> > g.group\_name } %\>
> > 
> > Look at the html generated by this to check that it is being included  
> > correctly.
> > 
> > Colin
> 
> Thank you Colin
> 
> I got the output.

Can you please solve this error for me

undefined method `each’ for 4:Fixnum

---

<div class="post-metadata">

**Author:** ![drk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b5ac83/32.png) [@drk](https://www.ruby-forum.com/u/drk)\
**Post date:** [May 21, 2010, 12:35am UTC](https://www.ruby-forum.com/t/nill-object-error/188903/8 "2010-05-21T00:35:35Z")

</div>

Colin L. wrote:

> On 20 May 2010 17:19, Ravi D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > ï¿½"users" , :action =\> “showfriendslist” } %\>
> > 
> > ï¿½ \<%= hidden\_field\_tag :group\_name ï¿½%\>
> > 
> > \<% end %\>
> > 
> > I can see my groupname displaying in my page, but when I click on my  
> > groupname link, Im not able to pass ï¿½groupname to my controller.
> 
> Using a hidden field does not work as you are not in a form. That is  
> how to do it if you were submitting from a form. Instead just include  
> the name in the url - something like  
> \<%= link\_to\_remote g.group\_name, :update =\> “abc”, :url =\> {  
> :controller =\> “users” , :action =\> “showfriendslist”, :group\_name =\>  
> g.group\_name } %\>
> 
> Look at the html generated by this to check that it is being included  
> correctly.
> 
> Colin

Thank you Colin

I got the output.

---

<div class="post-metadata">

**Author:** ![drk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b5ac83/32.png) [@drk](https://www.ruby-forum.com/u/drk)\
**Post date:** [May 21, 2010, 8:57am UTC](https://www.ruby-forum.com/t/nill-object-error/188903/9 "2010-05-21T08:57:41Z")

</div>

On 20 May 2010 23:37, Ravi D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> …  
> Can you please solve this error for me
> 
> undefined method `each’ for 4:Fixnum

I am sure you can work it out for yourself. Look at the line of code  
that generates the error, presumably there is a call to ‘each’ there.  
The error says that you are trying to call each for the value 4 (which  
is of type Fixnum), presumably you expected it to be an Array. If you  
can’t see the problem by code inspection use ruby-debug to break in  
before that line and have a look at the variable you are calling each  
on.

Colin
