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 6018379
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T03:16:01+00:00 2026-05-23T03:16:01+00:00

I have a a table author_comments with a fields author_name, comment and brand id.

  • 0

I have a a table author_comments with a fields author_name, comment and brand id.

I would like to get the number (count) of records where the author has more than N (2) records for a given brand.

For example,

author_comments

author_name      comment                 brand

joel             "loves donuts"             1

joel             "loves cookies"            1

joel             "loves oranges"            1

fred             "likes bananas"            2

fred             "likes tacos"              2

fred             "likes chips"              2

joe              "thinks this is cool"      1

sally            "goes to school"           1

sally            "is smart"                 1

sally            "plays soccer"             1

In this case my query should return 2 for brand 1 and 1 for brand 2.

I’m interested in the best performing option here, not getting all the records from the db and sorting through them in ruby, I can do this. I’m looking for best way using active record constructs or sql.

Update:
Here is the SQL:

SELECT author_name, COUNT(*) AS author_comments
FROM fan_comments
WHERE brand_id =269998788
GROUP BY author_name
HAVING author_comments > 2;

Should I just do find_by_sql?

  • 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-23T03:16:02+00:00Added an answer on May 23, 2026 at 3:16 am

    You can define the same query using active record constructions:

    FanComments.all(
      :select => 'author_name, count(*) as author_comments', 
      :group => 'author_name', 
      :having => 'author_comments > 2') # in rails 2
    

    or:

    FanComments.
      select('author_name, count(*) as author_comments').
      group('author_name').
      having('author_comments > 2') # in rails 3
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have a table of data like this tableid author book pubdate 1
I have a comments table where I store the comment content, author id and
I have a table like: table: comments, with rows: id, author, content, replyto I
So here's my situation. I have a books table and authors table. An author
Assume I'm indexing a table of books. They have title, author, description, etc. I
I have table inside a div tab. The table has 40 rows in it
I have table with 50 entries (users with such details like Name Surname Location
I have table with some fields that the value will be 1 0. This
I have two simple models: Book, and Author Each Book has one Author, linked
I have a simple database structure like this: entries table +----+-----------+------------+-----------+------+ | id |

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.