# Inconsistent noMethod error

**URL:** <https://www.ruby-forum.com/t/inconsistent-nomethod-error/183855>\
**Category:** Rails\
**Created:** [February 21, 2010, 10:29pm UTC](https://www.ruby-forum.com/t/inconsistent-nomethod-error/183855 "2010-02-21T22:29:22Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [February 21, 2010, 10:29pm UTC](https://www.ruby-forum.com/t/inconsistent-nomethod-error/183855/1 "2010-02-21T22:29:22Z")

</div>

Hi There,

I’m following a tutorial to introduce a search feature.

It comprises of a series of boxes, (A-Z) which are links. These links  
when clicked, trigger the search code in index (in infocontroller.rb)

Code below:

Info Controller:  
def index

@title = “search”

@letters = “ABCDEFGHIJKLMNOPQRSTUVWXYZ”.split("")  
if params[:id]  
@initial = params[:id]  
infos = Info.find(:all,  
:conditions =\> [“last\_name like ?”, @initial  
+’%’],  
:order =\> “last\_name”)  
@users = infos.collect { |info| info.user }  
end  
end

The index page (where the search is performed)

Alphabetical Index \<% @letters.each do |letter| %\> \<% letter\_class = (letter == @initial) ? "letter\_current" : "letter" %\>

\<%= link\_to letter, { :action =\> “index”, :id =\> letter },  
:class =\> letter\_class %\>  
\<% end %\>

\<%= render :partial =\> “search” %\>

And the partial, rendered by the index page:

\<% if @users and not @users.empty? %\>

 \<% @users.each do |user| %\> 

| Name | Gender | Location |
| --- | --- | --- |
| \<%= link\_to user.info.last\_name, profile\_for(user) %\> \<% end %\> |

\<% end %\>

For some odd reason, when I click the letter ‘B’ It throws an  
exception: You have a nil object when you didn’t expect it!  
The error occurred while evaluating nil.info

however, if I select any other letter, I have no problems.

What’s going on?

Many Thanks

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [February 21, 2010, 10:54pm UTC](https://www.ruby-forum.com/t/inconsistent-nomethod-error/183855/2 "2010-02-21T22:54:54Z")

</div>

The only place .info is being called is here:  
\<%= link\_to user.info.last\_name, profile\_for(user) %\>  
@users must contain a nil object.

Looks like you may have an info record with last\_name beginning with B,  
that does not have a user (user\_id == nil)

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [February 21, 2010, 11:19pm UTC](https://www.ruby-forum.com/t/inconsistent-nomethod-error/183855/3 "2010-02-21T23:19:42Z")

</div>

Maybe you have added the Info-Table after you have already put some  
Data in your User-table. In this case, there can exist some resultsets  
without an info-part.

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [February 22, 2010, 10:24am UTC](https://www.ruby-forum.com/t/inconsistent-nomethod-error/183855/4 "2010-02-22T10:24:33Z")

</div>

On 21 February 2010 21:27, RubyonRails\_newbie  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> @users = infos.collect { |info| info.user }
> 
> For some odd reason, when I click the letter ‘B’ It throws an  
> exception: You have a nil object when you didn’t expect it!  
> The error occurred while evaluating nil.info

That’s not an inconsistent nomethod error… if _every_ time you click  
“B” it throws, it’s a _very_ consistent one!

It’s telling you, right there on the screen, that one of the values in  
the @users array is nil, and nil objects don’t have an “info” method.

Have a rummage through the methods for Array [1] when you have  
downtime, and spot what the “compact” method does… I think it’ll  
sort you out (if you amend your controller thus 🙂

@users = infos.collect { |info| info.user }.compact

[1] [class Array - RDoc Documentation](http://ruby-doc.org/core/classes/Array.html) Whenever I get stuck,  
I’m straight onto Google with “ruby api [classname]” to bring me  
straight to the reference page that should get me on the right track  
again ("sometime it’s “Rails api [classname/methodname]” if the thing  
I’m messing with isn’t pure Ruby.
