0

I have various associated model in my application. I have setup Draper to decorate associations upon initialization of the parent class.

I have the following classes:

Customer (has_many :accounts)
Account  (belongs_to :customer && has_many :facilities && has_one :contact)
Contact  (belongs_to :account)
Facility (belongs_to :account)

I initiate a decorator as follows:

@customer = Customer.find_by(:token => params[:token]).decorate

In my account_decorator the following is added:

decorates_association :contact
decorates_association :facilities

...and in my Customer Decorator I have:

decorates_association :accounts

The issue I'm experiencing is that an undefined method exception is shown for any method defined in the facility_decorator. The contact_decorator works perfectly fine. I have made sure that my AR mapping is right, so it can't be that.

Any suggestions?

HermannHH
  • 1,732
  • 1
  • 27
  • 57
  • 1
    Maybe there's an unrelated problem with `Facility` decorator. Have you tried removing the associations and just instantiating decorated `Facility`, just to eliminate that? – Ninjarabbi Feb 08 '15 at 21:42
  • @Ninjarabbi , I have tested instantiating Facility on it's own and using .decorate on it. This does seem to work in this case, as I can access the FacilityDecorator methods – HermannHH Feb 09 '15 at 14:53
  • by the looks of it there's no difference between `contact` and `facility`. It's hard to figure this out without seeing all the code. I'd try to eliminate factors by changing the relation between `account` and `contact` to be `has_many` as well and see if it still works. Then I'd try to replicate parts of code between these two models until I find what makes 'contact` work and `facility` not work. – Ninjarabbi Feb 09 '15 at 21:41

1 Answers1

0

I solved this problem by eager_loading the associations.

@customer = Customer.eager_load({ :account => [:facilities]}).find_by(:token => params[:token]).decorate

This also improved the overall query time, so I'm happy with the solution!

HermannHH
  • 1,732
  • 1
  • 27
  • 57