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

  • Home
  • SEARCH
  • 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 4114038
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T22:24:13+00:00 2026-05-20T22:24:13+00:00

I think this may be a pretty simple ActiveRecord problem to solve but if

  • 0

I think this may be a pretty simple ActiveRecord problem to solve but if I have a query like:

@store = Store.find params[:id]
@categories = @store.categories.all

where say @categories = @store.categories.all returns like 3 objects. I then want to do a query where I get all of the associated items that have that same category_id, I tried:

@categories.items

But it didn’t work, any ideas?

  • 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-20T22:24:14+00:00Added an answer on May 20, 2026 at 10:24 pm

    I think what you’re looking for is, say, if we have category 1, category 2, and category 3, you want all items that belong to any of those categories. Is that right?

    This could be solved via iteration or manual conditions on a single query, but it sounds like the job for has_many :through.

    class Store < ActiveRecord::Base
      has_many :items, :through => :categories
    end
    

    After that, @store.items should return all items that belong to any of the store’s categories. A store has a relationship with certain items through the category object.

    If you’re wary of creating that relationship (though it’s definitely the simplest and least fragile way to go), you could also do this procedurally. The naive approach would be to run the items method on each category and put the results all in one array, but that would run one query per category: the infamous N+1 problem. If there are 50 categories, then that’s 50 queries just to fetch the items. You’d be better off running just one query to find the items you want:

    @items = Item.where(:category_id => @categories.map(&:id)).all
    

    (I’m not totally sure on that being the Arel syntax for the classic SQL WHERE…IN clause, but I think that’s it.)

    @categories.map(&:id) returns an array of all the categories’ IDs, which is passed as the condition for the item’s category_id. So, in English: “find all items where category_id is in this list of category IDs”

    So, there are two one-query solutions that perform about equally well. Go for the one that feels cleaner to you 🙂

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

Sidebar

Related Questions

You may think this question is like this question asked on StackOverflow earlier. But
I think this must be simple but I can't get it right... I have
I think this is a pretty simple question but I'm having trouble finding the
This ought to be pretty simple, maybe use a regex but I would think
I think this is pretty typical, you have the same website project with an
I know this question seems subjective but it's really pretty simple. As a long
This may be quite simple but I'm rather new to Lambda's so bear with
This may be a basic question but I am pretty new to DDD. I
This isn't quite as straight forward as one may think. I'm using a plugin
I think this is specific to IE 6.0 but... In JavaScript I add a

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.