# Activerecord not working

**URL:** <https://www.ruby-forum.com/t/activerecord-not-working/114327>\
**Category:** Rails\
**Created:** [September 11, 2007, 12:49pm UTC](https://www.ruby-forum.com/t/activerecord-not-working/114327 "2007-09-11T12:49:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dodgyboz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b19c9b/32.png) [@dodgyboz](https://www.ruby-forum.com/u/dodgyboz)\
**Post date:** [September 11, 2007, 12:49pm UTC](https://www.ruby-forum.com/t/activerecord-not-working/114327/1 "2007-09-11T12:49:19Z")

</div>

Hi all,  
I have a table like the one below called key\_value\_pairs

id spec val  
1 size small  
2 size medium  
3 size small

In my controller i have

def edit  
@pair = KeyValuePair.find(params[:id])  
end

and in my view i have such code as \<%= @pair.val %\>.

This code was all working as expected before (before what i’m not  
sure, but now it no longer works) as far as im aware i have made no  
changes to the edit method in the controller or any changes to the  
view and all the data is still in the database table but now when i  
try and view the page i get nil object error message

You have a nil object when you didn’t expect it!  
The error occurred while evaluating nil.val

Does any one have any suggestions or ideas? has this happened to  
anyone else?

I am not sure how to proceed as all my code (which is rather simple)  
looks like it should work.

---

<div class="post-metadata">

**Author:** ![dodgyboz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b19c9b/32.png) [@dodgyboz](https://www.ruby-forum.com/u/dodgyboz)\
**Post date:** [September 11, 2007, 8:57pm UTC](https://www.ruby-forum.com/t/activerecord-not-working/114327/2 "2007-09-11T20:57:38Z")

</div>

nil object is what you get when the you get no results.

Check for nil in your view, or adjust your controller to not send a nil.

\<%= @pair.val unless @pair.nil? -%\>

OR

# Don’t set @pair to a nil, ever

def edit  
value = KeyValuePair.find\_by\_id(params[:id])  
@pair value || KeyValuePair.new # You’ll get an empty object if value  
is nil.  
end

dodgyboz wrote:

> Hi all,  
> I have a table like the one below called key\_value\_pairs
> 
> id spec val  
> 1 size small  
> 2 size medium  
> 3 size small
> 
> In my controller i have
> 
> def edit  
> @pair = KeyValuePair.find(params[:id])  
> end
> 
> and in my view i have such code as \<%= @pair.val %\>.
> 
> This code was all working as expected before (before what i’m not  
> sure, but now it no longer works) as far as im aware i have made no  
> changes to the edit method in the controller or any changes to the  
> view and all the data is still in the database table but now when i  
> try and view the page i get nil object error message
> 
> You have a nil object when you didn’t expect it!  
> The error occurred while evaluating nil.val
> 
> Does any one have any suggestions or ideas? has this happened to  
> anyone else?
> 
> I am not sure how to proceed as all my code (which is rather simple)  
> looks like it should work.

---

<div class="post-metadata">

**Author:** ![dodgyboz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b19c9b/32.png) [@dodgyboz](https://www.ruby-forum.com/u/dodgyboz)\
**Post date:** [September 11, 2007, 11:04pm UTC](https://www.ruby-forum.com/t/activerecord-not-working/114327/3 "2007-09-11T23:04:31Z")

</div>

On 9/11/07, dodgyboz [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> In my controller i have  
> view and all the data is still in the database table but now when i  
> try and view the page i get nil object error message
> 
> You have a nil object when you didn’t expect it!  
> The error occurred while evaluating nil.val
> 
> Does any one have any suggestions or ideas? has this happened to  
> anyone else?

There’s not enough information to know what the problem is. The find()  
should raise an exception if the record isn’t found, so I suspect that  
for some reason, the find() isn’t being performed, and thus @pair  
isn’t being set.

One quick thing you can do is to temporarily add this to your  
controller right after the find:

raise @pair.inspect

Then call your action from the browser. If you still get the view  
error, then the find didn’t happen.
