This project is archived and is in readonly mode.

#4360 ✓resolved
ieuser

Chaining where with select "count * as c" in rails3 is failing

Reported by ieuser | April 10th, 2010 @ 06:13 AM

given

b=Business.where("businesses.is_public=?",true)
b=b.select("businesses., count() as c").joins(:contacts).group("business_id").order("c DESC")

should generate the sql::

using b.to_sql

SELECT businesses., count() as c FROM businesses INNER JOIN contacts ON contacts.business_id = businesses.id WHERE (businesses.is_public=1) GROUP BY business_id ORDER BY c DESC

but fails to execute given you try then access any record

ActiveRecord::StatementInvalid: Mysql::Error: Unknown column 'c' in 'order clause': SELECT COUNT(*) AS count_all, business_id AS business_id FROM businesses INNER JOIN contacts ON contacts.business_id = businesses.id WHERE (businesses.is_public=1) GROUP BY business_id ORDER BY c DESC

ths sql produced is different the original b.to_sql which is correct (run from a mysql console this works fine)

using arel 0.3.3 rails / 3.0.0beta2

Comments and changes to this ticket

  • Santiago Pastorino

    Santiago Pastorino April 12th, 2010 @ 04:33 PM

    is working fine for me can you test again with the latest versions of Rails and Arel.
    Thank you.

  • ronin-20016 (at lighthouseapp)

    ronin-20016 (at lighthouseapp) April 12th, 2010 @ 09:21 PM

    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

    Santiago Pastorino April 12th, 2010 @ 11:40 PM

    • State changed from “new” to “resolved”

    i don't follow what thing didn't work for you, but anyways you resolved it ;).

  • ieuser

    ieuser April 13th, 2010 @ 07:40 AM

    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

    ieuser April 13th, 2010 @ 07:45 AM

    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)

    ronin-20016 (at lighthouseapp) April 13th, 2010 @ 02:48 PM

    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

    ieuser April 13th, 2010 @ 07:08 PM

    You're correct re the scenario

    at the b.to_sql stage it gives the error

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Pages