0

I am working on this application that uses inheritance on models, but it is getting this error message on the view:

superclass mismatch for class BankTransfer

I have created an MVC called “payment_methods” Using the scaffold command:

rails g scaffold payment_methods type:string is_active:boolean

In the model file, payment_method.rb, I have included the classes:

class PaymentMethod < ApplicationRecord

end

class BankTransfer < PaymentMethod

end

class PayPal < PaymentMethod
end

Using rails console, with the commands: BankTransfer.create and Paypal.create, was possible to create on database the objects.

So running on rails console: PaymentMethod.all i get the return (that shows me that it was correctly save on the database):

PaymentMethod.all
  PaymentMethod Load (0.2ms)  SELECT "payment_methods".* FROM "payment_methods"                                                                 
 =>                                                                  
[#<Boleto:0x00007f995d403000                                         
  id: 1,                                                             
  type: "BankTransfer",                                                    
  is_active: nil,                                                    
  created_at: Fri, 15 Jul 2022 16:46:29.759160000 UTC +00:00,        
  updated_at: Fri, 15 Jul 2022 16:46:29.759160000 UTC +00:00>,       
 #<Stripe:0x00007f995d402da8
  id: 2,
  type: "PayPal",
  is_active: nil,
  created_at: Fri, 15 Jul 2022 16:46:56.087921000 UTC +00:00,
  updated_at: Fri, 15 Jul 2022 16:46:56.087921000 UTC +00:00>] 

The views and the controller were created automatically.

index.html.erb

<div id="payment_methods">
  <% @payment_methods.each do |payment_method| %>
    <%= render payment_method %>
    <p>
      <%= link_to "Show this payment method", payment_method %>
    </p>
  <% end %>
</div>

payment_methods_controller.rb

class PaymentMethodsController < ApplicationController
  before_action :set_payment_method, only: %i[ show edit update destroy ]

  def index
    @payment_methods = PaymentMethod.all
  end

  def show
  end

  def new
    @payment_method = PaymentMethod.new
  end

(...)

  private
    def set_payment_method
      @payment_method = PaymentMethod.find(params[:id])
    end

    def payment_method_params
      params.require(:payment_method).permit(:type, :is_active)
    end
end

What should I fix to be able to see a list of payment methods on the frontend. Instead of the error message superclass mismatch for class.

5
  • Hi @muistooshortm, thanks for reply. Currently, it has no "Stripe" I will implement the API interaction inside the class later. In the beginning, I need to be able to load the different objects that were saved in the database in the frontend. For example, I could change "Stripe" to "BankTransfer", then in the frontend, I would have a simple table with. | id | type | is_active | columns| , when I call PaymentMethod.all Commented Jul 15, 2022 at 18:36
  • I am trying to implement this solution for an inheritance, stackoverflow.com/questions/45339248/…, but getting an error message. Commented Jul 15, 2022 at 18:41
  • I hope that I could explain the issue, but if needed, I can add more information. Commented Jul 15, 2022 at 18:54
  • Sorry for the "Strype" spelling, It was an error when creating the issue. Changed to "BankTransfer" for a better explanation. Commented Jul 15, 2022 at 18:57
  • I deleted the database and changed the names for StripePaymentMethod, created a new database, and it worked. Thanks, if you want to post the answer I will mark it as solved, thanks. Commented Jul 15, 2022 at 19:31

1 Answer 1

1

If you're going to have a 'Stripe' value in your payment_methods.type column then you'll need to have a Stripe class that looks like this:

class Stripe < PaymentMethod
  # ...
end

Similarly for other payment_methods.type values.

The problem is that the stripe gem defines module Stripe, that's where your "superclass mismatch" error comes from.

The easiest solution would be to use a different name for your Stripe class, maybe StripePaymentMethod (and then PayPalPaymentMethod, BankTransferPaymentMethod, ... for consistency).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.