# Nil.update\_attributes on variable from find\_by

**URL:** <https://www.ruby-forum.com/t/nil-update-attributes-on-variable-from-find-by/123237>\
**Category:** Rails\
**Created:** [December 15, 2007, 9:39am UTC](https://www.ruby-forum.com/t/nil-update-attributes-on-variable-from-find-by/123237 "2007-12-15T09:39:12Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![lgas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a87d85/32.png) [@lgas](https://www.ruby-forum.com/u/lgas)\
**Post date:** [December 15, 2007, 9:39am UTC](https://www.ruby-forum.com/t/nil-update-attributes-on-variable-from-find-by/123237/1 "2007-12-15T09:39:12Z")

</div>

I have a pre-populated Factor row that belongs to a Job (Job has one  
Factor). The upd\_factor form looks fine – brings in all expected  
Factor data for that Job, but when I hit submit after making changes, it  
throws the error at the end of this message.

def upd\_factor  
@the\_factor = Factor.find\_by\_job\_id(params[:job\_id])  
if request.post?  
@the\_factor.update\_attributes(params[:the\_factor])

# redirect\_to :action =\> ‘index’, :job\_id =\> 22

```
end

```

end

(I commented out the one line to put focus on the update\_attributes  
line…) seems like what i’m trying to do is simple enough – any ideas  
on what i’m missing?

MESSAGE:

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.update\_attributes

Thanks in advance!

-jp

---

<div class="post-metadata">

**Author:** ![lgas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a87d85/32.png) [@lgas](https://www.ruby-forum.com/u/lgas)\
**Post date:** [December 15, 2007, 11:30am UTC](https://www.ruby-forum.com/t/nil-update-attributes-on-variable-from-find-by/123237/2 "2007-12-15T11:30:19Z")

</div>

On Dec 15, 8:39 am, John E. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> I have a pre-populated Factor row that belongs to a Job (Job has one  
> Factor). The upd\_factor form looks fine – brings in all expected  
> Factor data for that Job, but when I hit submit after making changes, it  
> throws the error at the end of this message.
> 
> def upd\_factor  
> @the\_factor = Factor.find\_by\_job\_id(params[:job\_id])  
> if request.post?  
> @the\_factor.update\_attributes(params[:the\_factor])

What is params[:job\_id]. Is it what you think it is (the fact that  
find\_by\_job\_id is returning nil suggests that it isn’t)

Fred

---

<div class="post-metadata">

**Author:** ![lgas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a87d85/32.png) [@lgas](https://www.ruby-forum.com/u/lgas)\
**Post date:** [December 16, 2007, 1:40am UTC](https://www.ruby-forum.com/t/nil-update-attributes-on-variable-from-find-by/123237/3 "2007-12-16T01:40:02Z")

</div>

Either use a ruby-debug gem and debug your code or do a puts @the\_factor  
in  
your code and check the development log file.

The variable @the\_factor is nil, the error message has nothing to do  
with  
update\_attributes.

On Dec 15, 2007 9:12 AM, John E. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![lgas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a87d85/32.png) [@lgas](https://www.ruby-forum.com/u/lgas)\
**Post date:** [December 15, 2007, 6:12pm UTC](https://www.ruby-forum.com/t/nil-update-attributes-on-variable-from-find-by/123237/4 "2007-12-15T18:12:55Z")

</div>

Frederick C. wrote:

> What is params[:job\_id]. Is it what you think it is (the fact that  
> find\_by\_job\_id is returning nil suggests that it isn’t)
> 
> Fred

The Factor form is populated as i expect it to w/ the find\_by\_id line  
(and if I take that line out it breaks), so I expect that  
params[:job\_id] is working – it’s the update\_attributes line that is  
killing me.  
Earlier, before I added the concept of Job to the program (and therefore  
had only one Factor and no need to find the correct Factor for the form)  
it worked as expected. Now I have to use the find\_by\_job\_id to get the  
correct Factor, and the update\_attributes blows up.

Sorry, that was a little wordy – hopefully I’m making sense…

Thanks!
