# Accessing a model from controller not working

**URL:** <https://www.ruby-forum.com/t/accessing-a-model-from-controller-not-working/142431>\
**Category:** Rails\
**Created:** [July 7, 2008, 10:49pm UTC](https://www.ruby-forum.com/t/accessing-a-model-from-controller-not-working/142431 "2008-07-07T22:49:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ptremblett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/dc4da7/32.png) [@ptremblett](https://www.ruby-forum.com/u/ptremblett)\
**Post date:** [July 7, 2008, 10:49pm UTC](https://www.ruby-forum.com/t/accessing-a-model-from-controller-not-working/142431/1 "2008-07-07T22:49:07Z")

</div>

Hi could someone please put me right. I am not having any success at  
accessing my model from a controller.Please look at the code:

Model:

class Region \< ActiveRecord::Base

def self.get\_region\_code(region)  
find\_by\_region("#{region}")  
end

end

Controller code:

@region = Region.get\_region\_code(@region\_name)

When I try to access the contents of @region

puts @region.region

It blows up with:

NoMethodError in Admin supplierController#update

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

Thankyou very much.

Paul Thompson.

---

<div class="post-metadata">

**Author:** ![ptremblett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/dc4da7/32.png) [@ptremblett](https://www.ruby-forum.com/u/ptremblett)\
**Post date:** [July 7, 2008, 10:52pm UTC](https://www.ruby-forum.com/t/accessing-a-model-from-controller-not-working/142431/2 "2008-07-07T22:52:38Z")

</div>

Hi –

On Tue, 8 Jul 2008, Paul Jonathan T. wrote:

> end  
> If region is a string, then you don’t need to quote and interpolate  
> it. That will just produce the same string again.

I’m not sure why you need to wrap find\_by\_region. Can’t you just use  
that in the controller?

> It blows up with:
> 
> NoMethodError in Admin supplierController#update
> 
> You have a nil object when you didn’t expect it!  
> The error occurred while evaluating nil.region

That means that your call to Region.get\_region\_code returned nil.  
You’d need to do some inspecting to see what’s in @region\_name at the  
time of the call.

David

–  
Rails training from David A. Black and Ruby Power and Light:  
Intro to Ruby on Rails July 21-24 Edison, NJ  
Advancing With Rails August 18-21 Edison, NJ  
See [http://www.rubypal.com](http://www.rubypal.com) for details and updates!

---

<div class="post-metadata">

**Author:** ![ptremblett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/dc4da7/32.png) [@ptremblett](https://www.ruby-forum.com/u/ptremblett)\
**Post date:** [July 7, 2008, 11:29pm UTC](https://www.ruby-forum.com/t/accessing-a-model-from-controller-not-working/142431/3 "2008-07-07T23:29:30Z")

</div>

Hi David,

Thank you for your advice. It worked when I changed the code in the  
controller to:

@region = Region.find\_by\_region(@region\_name)

rather than the previous code. But now I am confused, because I would  
have said that I am doing the identical thing. Where is my thinking  
wrong? As I have used the previous method before (with a different  
model and controller) and it worked just fine. The reason that I used  
the previous method was that I thought it was the correct way to do  
it.

Regards,  
Paul

2008/7/8 David A. Black [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

---

<div class="post-metadata">

**Author:** ![ptremblett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/dc4da7/32.png) [@ptremblett](https://www.ruby-forum.com/u/ptremblett)\
**Post date:** [July 8, 2008, 12:12am UTC](https://www.ruby-forum.com/t/accessing-a-model-from-controller-not-working/142431/4 "2008-07-08T00:12:59Z")

</div>

Paul Jonathan T. wrote:

> Hi David,
> 
> Thank you for your advice. It worked when I changed the code in the  
> controller to:
> 
> @region = Region.find\_by\_region(@region\_name)
> 
> rather than the previous code. But now I am confused, because I would  
> have said that I am doing the identical thing. Where is my thinking  
> wrong? As I have used the previous method before (with a different  
> model and controller) and it worked just fine. The reason that I used  
> the previous method was that I thought it was the correct way to do  
> it.
> 
> Regards,  
> Paul
> 
> 2008/7/8 David A. Black [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

def self.get\_region\_code(region)  
find\_by\_region(“#{region}”)  
end

is only different to just self.find\_by\_region(region) in the case that  
region is not a string,  
namely if it’s a nil, I imagine.

Region.find\_by\_region(nil) will do the SQL query “SELECT \* FROM regions  
where regions.region IS NULL”

while  
Region.get\_region\_code(nil) will do the SQL query “SELECT \* FROM regions  
WHERE regions.region = ‘’”

So I imagine that’s your difference.

There is no region with region == ‘’, while there is one with region ==  
nil  
?
