# Error prevention by empty DB's request

**URL:** <https://www.ruby-forum.com/t/error-prevention-by-empty-dbs-request/143243>\
**Category:** Rails\
**Created:** [July 16, 2008, 6:05pm UTC](https://www.ruby-forum.com/t/error-prevention-by-empty-dbs-request/143243 "2008-07-16T18:05:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![chaosbanane](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a698b9/32.png) [@chaosbanane](https://www.ruby-forum.com/u/chaosbanane)\
**Post date:** [July 16, 2008, 6:05pm UTC](https://www.ruby-forum.com/t/error-prevention-by-empty-dbs-request/143243/1 "2008-07-16T18:05:00Z")

</div>

Hi

I have often the problem that rails outputs an error because of requests  
from an empty database.

I will check, before showing features on the page, if a flag is set in  
the database. The problem is, that the DB is not filled, if you dont add  
the flag before. The result if i am doing the request is a nil object.

## For example:

if @tourscheduler\_user.tourscheduler\_id == @tourscheduler.id then  
‘bla’  
else  
‘bla2’  
end

* * *

The idee is: If there is a entry in @tourscheduler\_user with the same id  
then do else.

How can i check before if @tourscheduler\_user.tourscheduler\_id is empty?

Something like != nil ?

Greetings Andi

---

<div class="post-metadata">

**Author:** ![chaosbanane](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a698b9/32.png) [@chaosbanane](https://www.ruby-forum.com/u/chaosbanane)\
**Post date:** [July 16, 2008, 6:19pm UTC](https://www.ruby-forum.com/t/error-prevention-by-empty-dbs-request/143243/2 "2008-07-16T18:19:04Z")

</div>

nil returns false, so

if @tourscheduler\_user.tourscheduler\_id &&  
@tourscheduler\_user.tourscheduler\_id == @tourscheduler.id then  
…id and matching…  
else  
…no id or not matching…  
end

But there are several issues with this approach.

1. if you use tourscheduler\_id, then there should always be a  
tourscheduler matching it  
Otherwise your db is inconsistent. If you delete a tourscheduler you  
should make  
sure, it’s not referenced anymore…

2. It’s not very rails like  
You should define  
belongs\_to :tourscheduler  
in tourscheduler\_user model  
and  
has\_many :tourscheduler\_users  
in tourscheduler model and use the rails methods to handle  
the ralationship.  
Then your code would be simply:

if @tourscheduler\_user.tourscheduler then  
…  
else  
…  
end

---

<div class="post-metadata">

**Author:** ![chaosbanane](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a698b9/32.png) [@chaosbanane](https://www.ruby-forum.com/u/chaosbanane)\
**Post date:** [July 17, 2008, 8:36pm UTC](https://www.ruby-forum.com/t/error-prevention-by-empty-dbs-request/143243/3 "2008-07-17T20:36:59Z")

</div>

hi Thorsten

Thanks a lot for the answere!!!

## I follow your two points but the problem is still there.

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

Let me explain the idea:

There is Tourscheduler view where you can create trips with serval  
informations. In the view can users join the trip and announce that they  
come alonge. My solution is an entry with the user\_id and the  
tourscheduler\_id in the tourscheduler\_user DB if the user press a button  
in the tourpreview.

The result is an empty tourscheduler\_user DB till some user join the  
trip.

The query I use to decide if the “join button” is shown or not.

Maybee you have a better idea to solve the join issue?

greetings Andi
