Forum: Ruby on Rails Equivalent ruby syntax for this query.

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
m. j. (Guest)
on 2008-12-30 12:04
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
Frederick C. (Guest)
on 2008-12-30 12:11
(Received via mailing list)
On Dec 30, 10:04 am, rails and rails only <rails-mailing-l...@andreas-
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
Frederick C. (Guest)
on 2008-12-30 12:13
(Received via mailing list)
On Dec 30, 10:10 am, Frederick C. <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
This topic is locked and can not be replied to.