Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

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

#4360

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

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

State: resolved
Milestone: none
Assigned to: Aaron Patterson Aaron Patterson
Importance: none

Activity

  1. Santiago Pastorino
    Santiago Pastorino

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

    April 12th, 2010 @ 04:33 PM

  2. ronin-20016 (at lighthouseapp)
    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

    April 12th, 2010 @ 09:21 PM

  3. Santiago Pastorino
    Santiago Pastorino
    • State changed from new to resolved

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

    April 12th, 2010 @ 11:40 PM

  4. ieuser
    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")

    April 13th, 2010 @ 07:40 AM

  5. ieuser
    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")

    April 13th, 2010 @ 07:45 AM

  6. ronin-20016 (at lighthouseapp)
    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?

    April 13th, 2010 @ 02:48 PM

  7. ieuser
    ieuser

    You're correct re the scenario

    at the b.to_sql stage it gives the error

    April 13th, 2010 @ 07:08 PM