1

I'm working with Spree 2.1 and trying to add new payment gateway, but this error is more generic so Spree itself is not-so-important here.

I have encountered that error (undefined method 'association_class' for nil:NilClass) after adding some module to Spree::PaymentMethod (source) class:

spree/payment_method_decorator.rb

Spree::PaymentMethod.class_eval do
  include Spree::Core::CalculatedAdjustments
end

(Spree::Core::CalculatedAdjustments source)

(Spree::Gateway source)

Unfortunately, now Spree::PaymentMethod (source) breaks a little, i.e:

n = Spree::PaymentMethod.first
=> #<Spree::Gateway::Bogus id: 1, (...)>
n.save
=> undefined method 'association_class' for nil:NilClass
n.calculator
=> undefined method 'association_class' for nil:NilClass

Does anyone know why this happens and how to fix it?

In fact I already have an answer (after few hours of struggle), but maybe someone will give a better one with proper explanation. Maybe the answer is quite obvious, but it wasn't for me and I couldn't find anything related on SO so hopefully someone else with similar level of RoR knowledge won't have to spend another few hours on that.

zrl3dx
  • 7,699
  • 3
  • 25
  • 35
  • What did you do to fix it? Can you answer your own question? I ran into the same problem. – maikel May 11 '16 at 04:43
  • @maikel sorry for late response, I had to dig in my old laptop for that project ;) I have posted answer below. – zrl3dx May 15 '16 at 14:51

1 Answers1

0

So the answer was:

As it is visible in above links, Spree::Gateway is inheriting from Spree::PaymentMethod and my custom payment method was inheriting from Spree::Gateway class - something like:

module Spree class Gateway::CustomMethod < Gateway end end

All I had to do was to include Spree::Core::CalculatedAdjustments in Spree::Gateway:

Spree::Gateway.class_eval do include Spree::Core::CalculatedAdjustments end

and it is working since then.

zrl3dx
  • 7,699
  • 3
  • 25
  • 35