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?
So you want all order_lists for all customers associated with a user?
This will do the trick:
Or you could make a scope on OrderList that takes a set of customer_ids like so:
And then