# Custom update method problem

**URL:** <https://www.ruby-forum.com/t/custom-update-method-problem/115231>\
**Category:** Rails\
**Created:** [September 22, 2007, 1:56am UTC](https://www.ruby-forum.com/t/custom-update-method-problem/115231 "2007-09-22T01:56:07Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![bob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ea666f/32.png) [@bob](https://www.ruby-forum.com/u/bob)\
**Post date:** [September 22, 2007, 1:56am UTC](https://www.ruby-forum.com/t/custom-update-method-problem/115231/1 "2007-09-22T01:56:07Z")

</div>

I’ve got group of updates that I call in certain cases. They update  
data in different tables if specific conditions are met when a new row  
is saved to the “drive” (see below) database table. I suspect that I  
should make a method out of the group and reuse it in all the cases,  
but when I do that I’m getting errors that one of the objects is null.  
Here’s what the method should look like, but it’s not functioning when  
called.

def goal\_achieved\_update  
goal.update\_attributes (:achieved =\> 1,  
:achieved\_on =\> @drive.drive\_date)  
@best\_drive.update\_attributes (:state =\> 1)  
@drive.update\_attributes (:state =\> 8)  
end

Should I be passing arguments to this method? Or perhaps it should be  
in the model and not in the controller?

Any direction to sample code where a update method is created are  
reused would be greatly appreciated.

Thanks for any help!!  
Bob.

---

<div class="post-metadata">

**Author:** ![bob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ea666f/32.png) [@bob](https://www.ruby-forum.com/u/bob)\
**Post date:** [September 23, 2007, 9:34am UTC](https://www.ruby-forum.com/t/custom-update-method-problem/115231/2 "2007-09-23T09:34:11Z")

</div>

Bob wrote:

> I’ve got group of updates that I call in certain cases. They update  
> data in different tables if specific conditions are met when a new row  
> is saved to the “drive” (see below) database table. I suspect that I  
> should make a method out of the group and reuse it in all the cases,  
> but when I do that I’m getting errors that one of the objects is null.  
> Here’s what the method should look like, but it’s not functioning when  
> called.
> 
> def goal\_achieved\_update  
> goal.update\_attributes (:achieved =\> 1,  
> :achieved\_on =\> @drive.drive\_date)  
> @best\_drive.update\_attributes (:state =\> 1)  
> @drive.update\_attributes (:state =\> 8)  
> end
> 
> Should I be passing arguments to this method? Or perhaps it should be  
> in the model and not in the controller?
> 
> Any direction to sample code where a update method is created are  
> reused would be greatly appreciated.
> 
> Thanks for any help!!  
> Bob.

Bob,

My guess is that goal is the null object; it is a local variable as  
written (without a preceding glyph), but is not initialized within this  
context. Perhaps you mean @goal if it is an instance variable?

David B. Williams  
[http://www.cybersprocket.com](http://www.cybersprocket.com)
