ActiveRecord :has_many and includes

Viewed 8887

I am using rails 4.2.0 - this are my Models:

class Customer < ApplicationRecord
  has_many :meters, -> { includes :contracts }
end

class Meter < ApplicationRecord
  belongs_to :customer
  has_many :contracts
end

class Contract < ApplicationRecord
  belongs_to :meter
end

Now, I want to display all Contracts of an Customer. According to the Rails Guides I should be able to do this:

@customer.meters.contracts

However, it doesn't work. This is the error message:

undefined method `contracts' for #<Meter::ActiveRecord_Associations_CollectionProxy:0x007fb0385730b0>

What is the best approach in this situation? Thanks for your help!

1 Answers
Related