This project is archived and is in readonly mode.
Chaining where with select "count * as c" in rails3 is failing
-
Santiago Pastorino
is working fine for me can you test again with the latest versions of Rails and Arel.
Thank you. -
ronin-20016 (at lighthouseapp)
Shouldn't you have a * in your statement if you want to count * as c?
The following works for me:
b=Business.where("businesses.is_public=?",true) b=b.select("businesses.*, count() as c").joins(:contacts).group("business_id").order("c DESC")Thank you
-
Santiago Pastorino
- State changed from new to resolved
i don't follow what thing didn't work for you, but anyways you resolved it ;).
-
ieuser
lol, yes there was a * in the statement, my typo in the post - and no it doesnt work still
b=b.select("businesses., count() as c").joins(:contacts).group("business_id").order("c DESC")
-
ieuser
seems like the * is removed/cleaned by markdown when I posted it, no typo at all :-), (need to add a space either side of the * then it appears correctly in this post)
b=b.select("businesses.*, count( * ) as c").joins(:contacts).group("business_id").order("c DESC")
-
ronin-20016 (at lighthouseapp)
I'd like to better understand what you are trying to accomplish?
I take it you have 2 models - Business and Contact. Contact belongs_to a Business. Business has_many Contacts.
So let's say you have businesses named "Business1", "Business2" and "Business3"
Business1 and Business2 is public. Business3 is not public.Let's say you also have contacts named "Contact1", "Contact2" "Contact3". "Contact1" belongs_to "Business1", "Contact2" belongs_to "Business2" and "Contact3 belongs_to "Business3".
From console you now do:
b=Business.where("businesses.is_public=?",true) b=b.select("businesses.*, count() as c").joins(:contacts).group("business_id").order("c DESC") b.to_sql b.each { |business| puts business.name }Running the above steps, at what point do you receive an error?
-
ieuser
You're correct re the scenario
at the b.to_sql stage it gives the error