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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:05:22+00:00 2026-05-27T00:05:22+00:00

Support I have two models for items and categories, in a many-to-many relation class

  • 0

Support I have two models for items and categories, in a many-to-many relation

class Item < ActiveRecord::Base
  has_and_belongs_to_many :categories 

class Category < ActiveRecord::Base
  has_and_belongs_to_many :items

Now I want to filter out categories which contain at least one items, what will be the best way to do this?

  • 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-27T00:05:23+00:00Added an answer on May 27, 2026 at 12:05 am

    please notice, what the other guys answererd is NOT performant!

    the most performant solution:

    better to work with a counter_cache and save the items_count in the model!

    scope :with_items, where("items_count > 0")
    
    
    has_and_belongs_to_many :categories, :after_add=>:update_count, :after_remove=>:update_count
    
    def update_count(category)
      category.items_count = category.items.count
      category.save
    end
    

    for normal “belongs_to” relation you just write

    belongs_to :parent, :counter_cache=>true
    

    and in the parent_model you have an field items_count (items is the pluralized has_many class name)

    http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html

    in a has_and_belongs_to_many relation you have to write it as your own as above

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

Sidebar

Related Questions

I have three models : class LevelTwoArea < ActiveRecord::Base has_many :places, dependent: :restrict end
I have two models class Employer(models.Model): name = models.CharField(max_length=300, blank=False) id = models.IntegerField() status
I'm investigating on how validates_presence_of actually works. Suppose I have two models class Project
I have two models for Publications and Employees: class Publication(models.Model): BOOK_CHAPTER = 1 ARTICLE
I have two doubts: 1. I would like to add some client side support
I have a problem with IE causing two errors: 1. Object doesn't support this
I know IE7 & IE8 supposedly have support for using multiple CSS class selectors,
I have been support a web app that is used by a user base
I have two models Site and User. When a user registers with the app
I have the following model classes declared by SQLAlchemy: class User(Base): id = Column(Integer,

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.