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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T19:45:58+00:00 2026-05-31T19:45:58+00:00

I have a very simple Rails app with a very simple relational database: Category

  • 0

I have a very simple Rails app with a very simple relational database: Category has many Samples. I’d simply like to load the categories that have X number of samples.

In plain SQL I would do something like this:

SELECT
    categories.*
FROM
    categories
JOIN
    (SELECT
        category_id, COUNT(*) as sample_count
    FROM
        samples
    GROUP BY
        category_id
    ) AS subselect
ON
    categories.id=subselect.category_id
WHERE
    subselect.sample_count = X; -- where X is whatever

That works just fine, by the way, but it’s not terribly Rails-like to use raw SQL. And obviously I’d like to get those categories as model instances, so:

How would I go about re-writing something like that to an ActiveRecord or Arel query? Is it even feasible, or should I go with the plain SQL? Is there perhaps an altogether simpler way of doing it?

  • 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-31T19:45:59+00:00Added an answer on May 31, 2026 at 7:45 pm

    A possible nice way would be to use counter_cache, as described on this page: http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html

    Add a column named samples_count to your Category model:

    add_column :categories, :samples_count, :integer
    

    In your Sample model update belongs_to as follows:

    belongs_to :category , :counter_cache => true 
    

    You can now use the count as a condition, for example:

    Category.where(:samples_count => 7)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a very simple Rails 3.1 app with basic stuffs like following:
I am following a tutorial for building a very simple rails app. I have
I have a very simple admin controller in my Rails 2 app that displays
I have built a very simple blog application using Ruby on Rails. New to
I have very simple select like this: SELECT * FROM table WHERE column1 IN
I have a very simple Java RMI Server that looks like the following: import
I've got a very simple Rails 3 app with a User model run by
I'm trying to do something very simple in my first Rails app (Rails 3)
Rails: 3.0.3 Ruby: 1.9.2 Trying to deserialize a very simple object using YAML.load or
I'm building an events app that is very simple, it has a title and

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.