# Using saved object in same controller

**URL:** <https://www.ruby-forum.com/t/using-saved-object-in-same-controller/126057>\
**Category:** Rails\
**Created:** [January 15, 2008, 3:51am UTC](https://www.ruby-forum.com/t/using-saved-object-in-same-controller/126057 "2008-01-15T03:51:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![gustav](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/919ad9/32.png) [@gustav](https://www.ruby-forum.com/u/gustav)\
**Post date:** [January 15, 2008, 3:51am UTC](https://www.ruby-forum.com/t/using-saved-object-in-same-controller/126057/1 "2008-01-15T03:51:55Z")

</div>

Hi, I’m having a bit of an IF-statement-problem that produces a  
NoMethodError if it’s included in my method, although there is a  
corresponding column in my table. What I want to do is check if an  
record exists, or else I would like to create a new one based on  
previously defined variables in my method.

def ait  
@country = ‘Some place warm’  
@location = Location.find\_by\_adress(@requested\_adress)  
if @location = nil  
@new\_location = Location.new  
@new\_location.adress = @requested\_adress  
@new\_location.country = @hostname  
@new\_location.save  
@location = @location\_ip  
end  
render :text =\> @location.country  
end

Without the if-statement, it all works fine but when it’s included I  
get:

NoMethodError in TestsController#cached\_hostname\_reaction  
You have a nil object when you didn’t expect it!  
The error occurred while evaluating nil.country

It seems to me that I’m missing some fundamental Ruby-aspect, but I  
can’t figure out what…

Many thanks!

---

<div class="post-metadata">

**Author:** ![gustav](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/919ad9/32.png) [@gustav](https://www.ruby-forum.com/u/gustav)\
**Post date:** [January 15, 2008, 4:04am UTC](https://www.ruby-forum.com/t/using-saved-object-in-same-controller/126057/2 "2008-01-15T04:04:22Z")

</div>

Got it, by rewriting the if-statement like this:

if Location.find\_by\_adress(@requested\_adress)  
@location = Location.find\_by\_adress(@requested\_adress)  
else  
@location = Location.new  
@location.adress = @requested\_adress  
@location.country = @hostname  
@location.save  
end

Is there a more elegant way to do it?

---

<div class="post-metadata">

**Author:** ![gustav](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/919ad9/32.png) [@gustav](https://www.ruby-forum.com/u/gustav)\
**Post date:** [January 15, 2008, 4:14am UTC](https://www.ruby-forum.com/t/using-saved-object-in-same-controller/126057/3 "2008-01-15T04:14:32Z")

</div>

if @location.nil?

not if @location = nil

On Jan 15, 2008 1:21 PM, Gu stav [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> if @location = nil  
> get:  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> >

–  
Ryan B.

> **[frozenplague.net](http://www.frozenplague.net)**
>
> This domain may be for sale!

Feel free to add me to MSN and/or GTalk as this email.
