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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T15:34:13+00:00 2026-06-13T15:34:13+00:00

I doing a rails project where I’m using a self joining one-to-many setup. I

  • 0

I doing a rails project where I’m using a self joining one-to-many setup.

I have a class called User, which has many customers.
Each customer has many order lists, like this:

class User < ActiveRecord::Base
  has_many :customers, class_name: 'User', foreign_key: 'owner_id'
  belongs_to :owner, class_name: 'User'
  has_many :order_lists, dependent: :destroy

Now when I opened my rails console. I tried to do this:

user.customers.order_lists

I got this:

NoMethodError: undefined method `order_lists'

While when I try to do this:

user.customers.first.order_lists

I do receive the order lists of that customer.
But how could I recieve all the orderlists of all my customers?

Anybody an idea?

  • 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-06-13T15:34:14+00:00Added an answer on June 13, 2026 at 3:34 pm

    So you want all order_lists for all customers associated with a user?

    This will do the trick:

    user.customers.map(&:order_lists).flatten
    

    Or you could make a scope on OrderList that takes a set of customer_ids like so:

    scope :for_customer_ids, ->(customer_ids) {
      where("user_id IN (?)", customer_ids)
    }
    

    And then

    OrderList.for_customer_ids(user.customer_ids)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my rails project, I have a query which finds the 10 most recent
I am doing a ruby on rails project in class with a group and
This is a Rails 3 project using jQuery 1.4.4. I have an index action
I have a Rails project which I neglected to build tests for (for shame!)
I am doing my first project using Ruby on Rails and need to display
I have a Rails 2.2 project in which I want to override the functionality
I have a rails project deployed on heroku which has only a production environment.
I have been doing project on rails and I am very new to it.
I have a Rails 3 project with Devise, confirmable enabled so the user has
Here is a app contorller directory from Rails project doing a self study for

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.