# Can someone suggest a better way to do this?

**URL:** <https://www.ruby-forum.com/t/can-someone-suggest-a-better-way-to-do-this/70915>\
**Category:** Rails\
**Created:** [October 3, 2006, 10:55pm UTC](https://www.ruby-forum.com/t/can-someone-suggest-a-better-way-to-do-this/70915 "2006-10-03T22:55:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e0b2c6/32.png) [@mic](https://www.ruby-forum.com/u/mic)\
**Post date:** [October 3, 2006, 10:55pm UTC](https://www.ruby-forum.com/t/can-someone-suggest-a-better-way-to-do-this/70915/1 "2006-10-03T22:55:40Z")

</div>

Heres a short snippet that I created. I wanted to make sure that the  
record existed, and it actually belonged to the user before trying to  
delete it.

for delEmp in employ  
@Count = ListCrew.count(:all, :conditions =\>  
[" saved\_by = ? AND employee\_ID = ? “, session[:employee\_id],  
delEmp.to\_i])  
if @Count \> 0  
crew = ListCrew.find(:first, :conditions =\>  
[” saved\_by = ? AND employee\_ID = ? ", session[:employee\_id],  
delEmp.to\_i])  
crew.destroy  
end #if  
end #for

It seems like there should be a way to do this with a single DB query  
instead of using 2. Any suggestions?

---

<div class="post-metadata">

**Author:** ![mic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e0b2c6/32.png) [@mic](https://www.ruby-forum.com/u/mic)\
**Post date:** [October 3, 2006, 11:32pm UTC](https://www.ruby-forum.com/t/can-someone-suggest-a-better-way-to-do-this/70915/2 "2006-10-03T23:32:52Z")

</div>

Why do you need to do the count first? Just do the find, check if it  
actually  
returns the valid object and delete it only in this case.

On Tuesday 03 October 2006 23:55, Mic wrote:

> ```
> [" saved_by = ? AND employee_ID = ? ", session[:employee_id],
> 
> ```
> 
> delEmp.to\_i])  
> crew.destroy  
> end #if  
> end #for
> 
> It seems like there should be a way to do this with a single DB query  
> instead of using 2. Any suggestions?

–  
WBR, Oleg Ivanov  
ICQ #69991809  
Jabber: removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![mic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e0b2c6/32.png) [@mic](https://www.ruby-forum.com/u/mic)\
**Post date:** [October 3, 2006, 11:40pm UTC](https://www.ruby-forum.com/t/can-someone-suggest-a-better-way-to-do-this/70915/3 "2006-10-03T23:40:54Z")

</div>

> [" saved\_by = ? AND employee\_ID = ? ", session[:employee\_id],  
> delEmp.to\_i])  
> crew.destroy  
> end #if  
> end #for
> 
> It seems like there should be a way to do this with a single DB query  
> instead of using 2. Any suggestions?

Maybe this? The find will only return an object if one matches the  
conditions, and your conditions seem to be the same from count to find,  
so if we find it, destroy. Wrap it in a begin/rescue/end in case we  
dont’ find it and it complains about that, the rescue will shut it up.

for delEmp in employ  
begin  
ListCrew.find(:first,  
:conditions =\> [“saved\_by = ? AND employee\_ID = ?”,  
session[:employee\_id],  
delEmp.to\_i]).destroy  
rescue  
nil  
end  
end
