Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4343468
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T11:45:26+00:00 2026-05-21T11:45:26+00:00

I have the following call in my RoR script: Foo.where(event = ‘Login’).group(‘user_id’).order(‘user_id ASC’).count() This

  • 0

I have the following call in my RoR script:

Foo.where("event = 'Login'").group('user_id').order('user_id ASC').count()

This gives me a list of all users and how much they have logged in in the form of:

<userid1> => <count>, <userid2> => <count>, ...}

This is great and very close to what I wan but I’ve been unable to convince it to sort by the count of logins instead, what I’d really like to have it do. There is also a column that has some info about the login session in the form of a character delimited string. I’d like to get at certain parts of that information.

To achieve this I’ve tried using find_by_sql and when I make the following call:

Foo.find_by_sql("SELECT userid, COUNT(*) AS number, SUBSTRING_INDEX(stuff, ',', 1) AS info FROM <table> WHERE event = 'Login' GROUP BY userid")

What I get is a ilst of Foo entries that contain the userids but not the count or the info. When I run this in the MySQL workbench it works like a charm. Is there something else I need to do to get this to work? Also, would there be a way to just do this using Foo.select or Foo.where? Thanks.

Update I have also tried this format, as demonstrated here:

Foo.find(:all, :select => 'count(*) count, userid', :group =>'userid')

But this too merely responds with the userids and does not spit out the count.

Update 2 Looking at the output a bit more i can see now that when i do the find_by_fql call everything is being found in the correct way and even being sorted. It just isn’t actually selecting the COUNT(*) or the SUBSTRING_INDEX.

Update 3 I also tried out this SO tip but when I tell it:

Foo.find(:all, :select => 'userid, count(*) as cnt', :group => 'userid')

It doesn’t print or find anything related to the var cnt. I’m totally baffled here because I’ve seen more than one example now that does it this ^^ way and I’ve yet to get it to succeed.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T11:45:26+00:00Added an answer on May 21, 2026 at 11:45 am

    Actually, your problem is not an SQL problem. To generate the correct SQL you would just need this:

    Foo.where("event = 'Login'").group('user_id').order('count_all').count()
    

    Take a look in your log and you’ll find that this generates the following SQL:

    SELECT COUNT(*) AS count_all, user_id AS school_id FROM `foos` GROUP BY user_id ORDER BY count_all
    

    …and if you run that in your SQL console you’ll get what you want.

    The problem is that Rails doesn’t return them in this order, Rails always returns these special group/count results in the order of the GROUP BY field. So, if you want them in a different order then you’ll need to do it in Ruby after getting the hash back.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following simple python test script that uses Suds to call a
I have following ajax call in my JavaScript code url = 'http://news.ycombinator.com/?callback=?'; $.ajax({url:url ,async:!1,dataType:'script',
I have a script (call it Main.R ) that has the following code to
I have the following call to load a grid: $(#searchlist).jqGrid({ url:'./searchlibrary', datatype: 'json', mtype:
I have the following ajax call var prev_sibling = $(this).prev().attr(value); var next_sibling = $(this).next().attr(value);
I have the following function call from a thread: Thread Move = new Thread(){
This is an MVC3 app. I have the following javascript call to my action:
I have the following problem: I make the call to the method of a
I have the following code to make a jsonp call. var contacts; $.ajax({ url:
I have following ajax call and response with json data type. Currently I write

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.