# Receiving nil on ID results using select

**URL:** <https://www.ruby-forum.com/t/receiving-nil-on-id-results-using-select/193291>\
**Category:** Rails\
**Created:** [August 15, 2010, 12:28am UTC](https://www.ruby-forum.com/t/receiving-nil-on-id-results-using-select/193291 "2010-08-15T00:28:32Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![elricstorm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/da6949/32.png) [@elricstorm](https://www.ruby-forum.com/u/elricstorm)\
**Post date:** [August 15, 2010, 12:28am UTC](https://www.ruby-forum.com/t/receiving-nil-on-id-results-using-select/193291/1 "2010-08-15T00:28:32Z")

</div>

On my teams table I have a column for ESPN names where ESPN uses it’s  
own naming convention for teams. I’m trying to do a simple  
find\_by\_field…

@team = “Navy”

@o\_team = Team.find\_by\_espn\_team(@team, :select =\> “teams.id AS  
opponent\_id”)  
p @o\_team[:opponent\_id]  
p @o\_team.opponent\_id

In the console it shows the correct number but right after posting the  
number it states:

Team = Navy | ESPN ID = 2426 | TEAM ID = 1  
61  
61

NoMethodError: 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.[]

61 is the correct ID for the first opponent and both puts show the  
correct number. The line this points at points to the first p  
@o\_team[:opponent\_id] and if I remove it and use the second it does the  
same, pointing to the second puts statement.

If I try a generic non-select query and use team.id or team\_id I receive  
a whiny nil error.

Any help would be appreciated.

Thanks.

---

<div class="post-metadata">

**Author:** ![elricstorm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/da6949/32.png) [@elricstorm](https://www.ruby-forum.com/u/elricstorm)\
**Post date:** [August 15, 2010, 4:25am UTC](https://www.ruby-forum.com/t/receiving-nil-on-id-results-using-select/193291/2 "2010-08-15T04:25:39Z")

</div>

Team.all(:conditions =\> {:espn\_team =\> @team}).each do |row|  
@o\_team = row.id  
end

p @o\_team

The .find\_by query was the cause. Swapping to all and applying a  
condition for the team works.
