# Equivalent ruby syntax for this query

**URL:** <https://www.ruby-forum.com/t/equivalent-ruby-syntax-for-this-query/156583>\
**Category:** Rails\
**Created:** [December 30, 2008, 11:04am UTC](https://www.ruby-forum.com/t/equivalent-ruby-syntax-for-this-query/156583 "2008-12-30T11:04:28Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mjayakumar2k6](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/dc4da7/32.png) [@mjayakumar2k6](https://www.ruby-forum.com/u/mjayakumar2k6)\
**Post date:** [December 30, 2008, 11:04am UTC](https://www.ruby-forum.com/t/equivalent-ruby-syntax-for-this-query/156583/1 "2008-12-30T11:04:28Z")

</div>

Book.find(:all,  
:conditions=\>[“release\_date \>= ? and release\_date \< ? and  
book\_type=‘New’”,@date[:from],@date[:to]],  
:group=\>“auther\_id”,  
:order=\>“count(_) desc",  
:select=\>"auther\_id,sum(case when auther\_type = ‘New’ "+  
"then 1 else 0 end) as new,sum(case when auther\_type != ‘New’ then  
1 else 0 end) as review,count(_) as total”)

in the above query how to write the “case” conditions in ruby query.

can anyone tell me which is the equivalent ruby syntax for this

sum(case when auther\_type != ‘New’ then 1 else 0 end) as review

thanks in advance  
Jk

---

<div class="post-metadata">

**Author:** ![mjayakumar2k6](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/dc4da7/32.png) [@mjayakumar2k6](https://www.ruby-forum.com/u/mjayakumar2k6)\
**Post date:** [December 30, 2008, 11:11am UTC](https://www.ruby-forum.com/t/equivalent-ruby-syntax-for-this-query/156583/2 "2008-12-30T11:11:10Z")

</div>

On Dec 30, 10:04 am, rails and rails only \<rails-mailing-l…@andreas-  
[s.net](http://s.net)\> wrote:

> Book.find(:all,  
> :conditions=\>[“release\_date \>= ? and release\_date \< ? and  
> book\_type=‘New’”,@date[:from],@date[:to]],  
> :group=\>“auther\_id”,  
> :order=\>“count(_) desc",  
> :select=\>"auther\_id,sum(case when auther\_type = ‘New’ "+  
> "then 1 else 0 end) as new,sum(case when auther\_type != ‘New’ then  
> 1 else 0 end) as review,count(_) as total”)
> 
> in the above query how to write the “case” conditions in ruby query.

You can’t - there are no helpers for generating complex select  
statements like that.

Fred

---

<div class="post-metadata">

**Author:** ![mjayakumar2k6](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/dc4da7/32.png) [@mjayakumar2k6](https://www.ruby-forum.com/u/mjayakumar2k6)\
**Post date:** [December 30, 2008, 11:13am UTC](https://www.ruby-forum.com/t/equivalent-ruby-syntax-for-this-query/156583/3 "2008-12-30T11:13:31Z")

</div>

On Dec 30, 10:10 am, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > 1 else 0 end) as review,count(\*) as total")
> 
> > in the above query how to write the “case” conditions in ruby query.
> 
> You can’t - there are no helpers for generating complex select  
> statements like that.

To clarify, there aren’t any builtin things. There might be a third  
party plugin that does this.

Fred
