# Returning an object that might be nil

**URL:** <https://www.ruby-forum.com/t/returning-an-object-that-might-be-nil/121776>\
**Category:** Rails\
**Created:** [November 30, 2007, 8:04pm UTC](https://www.ruby-forum.com/t/returning-an-object-that-might-be-nil/121776 "2007-11-30T20:04:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![krystal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f08c70/32.png) [@krystal](https://www.ruby-forum.com/u/krystal)\
**Post date:** [November 30, 2007, 8:04pm UTC](https://www.ruby-forum.com/t/returning-an-object-that-might-be-nil/121776/1 "2007-11-30T20:04:38Z")

</div>

So I have a question that I haven’t been able to find the answer to.

Let’s say I have an api method that :returns an ActiveRecord::Base  
object. In that class (say, Cat \< ActiveRecord::Base) I have a method  
that finds a cat given by an id and returns that record in the database.

so:

api\_method :findcat  
:expects =\> [{:catid}]  
:returns =\> [Cat]  
is the api call. The controller calls get\_cat in Cat \<  
ActiveRecord::Base, which does self.find(cat\_id).

Now, if there is no Cat with id cat\_id, the result will be nil,  
according to the documentation for ActiveRecord’s find.

Will things go boom if someone calls the api method with an non-existant  
id? Or will it return a Cat object that just happens to be nil?

I’m thinking the latter but really aren’t sure.

---

<div class="post-metadata">

**Author:** ![krystal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f08c70/32.png) [@krystal](https://www.ruby-forum.com/u/krystal)\
**Post date:** [November 30, 2007, 8:22pm UTC](https://www.ruby-forum.com/t/returning-an-object-that-might-be-nil/121776/2 "2007-11-30T20:22:27Z")

</div>

Don’t over-engineer your models in Rails.

You already get what you’re looking for automatically.

Given this class (entire contents)

class Cat \< ActiveRecord::Base  
end

You can do the following:

@cat = Cat.find(1)  
Will throw exception if you don’t pass in a valid id

@cat = Cat.find\_by\_id(1)  
returns nil

There’s no need for your own special API. This is why ActiveRecord  
exists -  
to keep you from having to write these methods yourself. #find and its  
variants are all public class methods. No need to wrap them 🙂

Hope that helps.

On Nov 30, 2007 1:04 PM, Erin K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![krystal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f08c70/32.png) [@krystal](https://www.ruby-forum.com/u/krystal)\
**Post date:** [December 1, 2007, 12:40am UTC](https://www.ruby-forum.com/t/returning-an-object-that-might-be-nil/121776/3 "2007-12-01T00:40:18Z")

</div>

This is really an oversimplification of what I want, but I do that  
because what I care about is retrieving an object. I find that for me  
when I am learning a new language, the simplest examples are the best to  
learn from.

Thanks for your answer.
