Home > OS >  Ruby: Associations to enable querying across 4 models
Ruby: Associations to enable querying across 4 models

Time:03-24

I've been trying all day to figure this out and not getting anywhere. I have these 4 models. Shop Customer Credit and CreditChange

I want to return the CreditChanges for a shop's customers, NOT the customers. Like this:

latest_changes = Shop.last.customers.credit_changes

I'm trying to join like this, but its returning the customers and not the credit_changes

Shop.last.customers.joins(:credit, :credit_change).where.not(credit_changes: {date: nil})

My models look like the following:

class Shop < ActiveRecord::Base
  has_many :customers, dependent: :destroy

class Customer < ActiveRecord::Base
  has_one :credit, dependent: :destroy
  has_many :credit_changes, through: :credit
  belongs_to :shop

class Credit < ActiveRecord::Base
  belongs_to :customer
  has_many :credit_changes, dependent: :destroy

class CreditChange < ActiveRecord::Base
  belongs_to :credit

Where did I go wrong?

CodePudding user response:

You want a has-many-through relationship.

A Shop has many CreditChanges through its Customers.

Try:

class Shop
  has_many :customers
  has_many :credit_changes, through: :customers
end

@shop.credit_changes

You can read more in the Rails documentation https://guides.rubyonrails.org/association_basics.html#the-has-many-through-association

CodePudding user response:

Try inverting the way you're looking at things:

CreditChange.joins(:credit => :customer).where(customers: {shop: Shop.last}).where.not(credit_changes: {date: nil})
  • Related