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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T04:33:41+00:00 2026-05-16T04:33:41+00:00

This is a beginner’s question but for some reason I cannot find the answer

  • 0

This is a beginner’s question but for some reason I cannot find the answer elsewhere.

Customer has_many orders  
Order has_many order_items

I am in customer/show.html.erb and I want my customer to manipulate order_items.

Many orders have many order_items and I want to search ALL of those order_items to find those such that read == false.

#controller
@customer = Customer.find(params[:id])
@orders = @customer.orders

@order_items = @orders.order_items doesn’t work. Given that I have multiple items in @orders, how can I collect all the order_items that belong to @orders?

=== EDIT ===

My entire database structure is a big complicated group of tables and I need to traverse that tree for this particular view.

customer has_many orders
orders has_many order_items
order_items belongs_to category

How do I, for example, find the number of my customer’s order_items that belong to category X?

Last question: why doesn’t @orders.find_all_by_x(…) work?

  • 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-16T04:33:42+00:00Added an answer on May 16, 2026 at 4:33 am

    1) Show all OrderItems for a customer

    You can add a relation called “order_items” to your customer like this:

    has_many :order_items, :through => :orders
    

    You can then do:

    customer = Customer.find(:first)
    order_items = customer.order_items
    

    To find all the unread OrderItems (where read == false), you can add a named_scope to your OrderItem:

    named_scope :unread, :conditions => {:read => false}
    

    you can then find all unread OrderItems for one customer by:

    customer.order_items.unread
    

    2) Show all OrderItems of a Customer belonging to a given Category

    Again, a named scope in OrderItem (I assume OrderItem belongs_to Category):

    named_scope :in_category, lambda { |name|
      {:conditions => {:categories => {:name => name}}, :include => :category}
    }
    

    Two Notes:

    1. You have to include :category in the conditon

    2. even though its belongs_to :category (singular), you have to use :categories (plural). This is because the :include adds the table as plural. (I don’t know why it does that, and for me this seems like a hint that this is not the best solution.)

    Last question: why doesn’t @orders.find_all_by_x(…) work?

    This does work as expected for me. What error message are you getting?

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

Sidebar

Related Questions

This may be a beginner xml question, but how can I generate an xml
i know this question was asked before but i have this very weird beginner
This is a beginner question, but please bear with me. I'd like to pass
I know this is a beginner's question, but I've been searching online and all
This is a beginner question, I have to create some SQL Reports with business
This is a beginner question, but it's been frustrating me... I am using C#,
I had this question (beginner C++) in a C++ quiz : My answer was
I know this is a beginner question, but I've been banging my head against
This is a beginner's question but how do you save a 2d numpy array
this is a beginner question- I can't seem to find any good resources explaining

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.