# Conditions either null or real value

**URL:** <https://www.ruby-forum.com/t/conditions-either-null-or-real-value/171530>\
**Category:** Rails\
**Created:** [July 4, 2009, 5:27am UTC](https://www.ruby-forum.com/t/conditions-either-null-or-real-value/171530 "2009-07-04T05:27:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![grayson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c77e96/32.png) [@grayson](https://www.ruby-forum.com/u/grayson)\
**Post date:** [July 4, 2009, 5:27am UTC](https://www.ruby-forum.com/t/conditions-either-null-or-real-value/171530/1 "2009-07-04T05:27:14Z")

</div>

if my category\_id could be either nil or a int value, how can I specify  
a condition that handles both?

conditions =\> [“parent\_id = ?”,params[:category\_id]]  
conditions =\> [“parent\_id is ?”,params[:category\_id]]

I’m using acts\_as\_tree which meets the root levels have no parent\_id  
(null in SQL)  
so if no category\_id is passed as a parameter I want it to pull up the  
roots, if one is passed, I want it to pull the children of that node.

---

<div class="post-metadata">

**Author:** ![grayson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c77e96/32.png) [@grayson](https://www.ruby-forum.com/u/grayson)\
**Post date:** [July 4, 2009, 8:36am UTC](https://www.ruby-forum.com/t/conditions-either-null-or-real-value/171530/2 "2009-07-04T08:36:41Z")

</div>

On Jul 3, 11:27 pm, Grayson P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> if my category\_id could be either nil or a int value, how can I specify  
> a condition that handles both?
> 
> conditions =\> [“parent\_id = ?”,params[:category\_id]]  
> conditions =\> [“parent\_id is ?”,params[:category\_id]]

It you do :conditions =\> {:parent\_id =\> params[:category\_id]}

it should handle the case where params[:category\_id] is nil.

Fred

---

<div class="post-metadata">

**Author:** ![grayson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c77e96/32.png) [@grayson](https://www.ruby-forum.com/u/grayson)\
**Post date:** [July 5, 2009, 7:44pm UTC](https://www.ruby-forum.com/t/conditions-either-null-or-real-value/171530/3 "2009-07-05T19:44:16Z")

</div>

Grayson P. wrote:  
[…]

> I’m using acts\_as\_tree which meets the root levels have no parent\_id  
> (null in SQL)  
> so if no category\_id is passed as a parameter I want it to pull up the  
> roots, if one is passed, I want it to pull the children of that node.

You should probably be using something like awesome\_nested\_set instead  
– it allows for much more efficient DB operations.

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid
