Forum: Ruby on Rails polymorphism + inheritance

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Peter E. (Guest)
on 2006-03-27 16:19
(Received via mailing list)
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
Don W. (Guest)
on 2006-03-27 20:29
(Received via mailing list)
Looks like you're trying to use single table inheritance here.

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

This requires a "payments" table with a "type" column for storing the
actual
class of the row ("CreditCardPayemnt" or "PayPalPayement" or ...)
This topic is locked and can not be replied to.