# Active record :find having an if statement inside the conditions

**URL:** <https://www.ruby-forum.com/t/active-record-find-having-an-if-statement-inside-the-conditions/155247>\
**Category:** Rails\
**Created:** [December 9, 2008, 9:01pm UTC](https://www.ruby-forum.com/t/active-record-find-having-an-if-statement-inside-the-conditions/155247 "2008-12-09T21:01:29Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![tyanlion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f0a364/32.png) [@tyanlion](https://www.ruby-forum.com/u/tyanlion)\
**Post date:** [December 9, 2008, 9:01pm UTC](https://www.ruby-forum.com/t/active-record-find-having-an-if-statement-inside-the-conditions/155247/1 "2008-12-09T21:01:29Z")

</div>

Hi, i was just wondering if its is possible to put a if statment  
inside the find conditions

for example:

grovery\_price = model.find(:first, :conditions =\> [ “if x = ? then y  
must = ?”, 200, 300]

I have been googling for some time for a solution to this but i just  
can’t seem to find any.

---

<div class="post-metadata">

**Author:** ![tyanlion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f0a364/32.png) [@tyanlion](https://www.ruby-forum.com/u/tyanlion)\
**Post date:** [December 9, 2008, 9:12pm UTC](https://www.ruby-forum.com/t/active-record-find-having-an-if-statement-inside-the-conditions/155247/2 "2008-12-09T21:12:21Z")

</div>

Tyl Tan wrote:

> Hi, i was just wondering if its is possible to put a if statment  
> inside the find conditions
> 
> for example:
> 
> grovery\_price = model.find(:first, :conditions =\> [ “if x = ? then y  
> must = ?”, 200, 300]
> 
> I have been googling for some time for a solution to this but i just  
> can’t seem to find any.

Does

:conditions =\> ["(x = ? and y = ?) or x \<\> ?", 200, 300, 200]

work?

---

<div class="post-metadata">

**Author:** ![tyanlion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f0a364/32.png) [@tyanlion](https://www.ruby-forum.com/u/tyanlion)\
**Post date:** [December 9, 2008, 9:14pm UTC](https://www.ruby-forum.com/t/active-record-find-having-an-if-statement-inside-the-conditions/155247/3 "2008-12-09T21:14:23Z")

</div>

Or is it just:

:conditions =\> ["(x = ? and y = ?)", 200, 300]

i.e., do you want the items where x != 200 or not?

---

<div class="post-metadata">

**Author:** ![tyanlion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f0a364/32.png) [@tyanlion](https://www.ruby-forum.com/u/tyanlion)\
**Post date:** [December 9, 2008, 9:19pm UTC](https://www.ruby-forum.com/t/active-record-find-having-an-if-statement-inside-the-conditions/155247/4 "2008-12-09T21:19:13Z")

</div>

If x equal 200 then I want the find to check that y is equal to 300 but  
if x is not equal to 200 there should be no check with y

---

<div class="post-metadata">

**Author:** ![tyanlion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f0a364/32.png) [@tyanlion](https://www.ruby-forum.com/u/tyanlion)\
**Post date:** [December 9, 2008, 9:32pm UTC](https://www.ruby-forum.com/t/active-record-find-having-an-if-statement-inside-the-conditions/155247/5 "2008-12-09T21:32:56Z")

</div>

Tyl Tan wrote:

> If x equal 200 then I want the find to check that y is equal to 300 but  
> if x is not equal to 200 there should be no check with y

Well,

:conditions =\> [“x \<\> ? or (x = ? and y = ?)”, 200, 200, 300]

If SQL short-circuits the logic, that shouldn’t check y when x != 200

---

<div class="post-metadata">

**Author:** ![tyanlion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f0a364/32.png) [@tyanlion](https://www.ruby-forum.com/u/tyanlion)\
**Post date:** [December 9, 2008, 9:39pm UTC](https://www.ruby-forum.com/t/active-record-find-having-an-if-statement-inside-the-conditions/155247/6 "2008-12-09T21:39:11Z")

</div>

Thanks I’ll try it out when I have the time
