Polymorphism + inheritance


#1

Hi,

currently I do some work with polymorphic associations in egde rails

class Order < ActiveRecord::Base
belongs_to :payment, :polymorphic => true
end

class CreditCardPayment < Payment
end

class PayPalPayment < Payment
end

class Payment < ActiveRecord::Base
has_one :order, :as => :payment

common stuff in here

end

I want to use [Payment] for common functionality typical to payments.

However, this will throw an error:

CreditCardPayment.create(…)
ActiveRecord::StatementInvalid: Mysql::Error: #42S02Table
‘demo.payments’
doesn’t exist: SHOW FIELDS FROM payments

Is there a way to convince ActiveRecord that [Payment] is an abstract
base
class that does not need to exist (and take the name of the concrete
class
for determining the table name) ?

Thanks in advance!!

Best regards
Peter


#2

Looks like you’re trying to use single table inheritance here.

http://wiki.rubyonrails.com/rails/pages/SingleTableInheritance

This requires a “payments” table with a “type” column for storing the
actual
class of the row (“CreditCardPayemnt” or “PayPalPayement” or …)