I'm trying to manage several means of payment in my Rails 3.2 application.
Let's say I have 2 means : Credit Cards & Bank Transfers (BIC), working with 2 different payment providers. Those means both contain a reference string that will be sent to the payment provider to trigger new payments later. Each means also contains specific information. This is why I'm thinking about creating 2 models (a CreditCard one and a Bic one). And I would like to link both models to the User model.
A single user can have for example 2 credit cards and 1 BIC so there must be at least a have_many association. I don't want my user model to have too many different associations, but I want it to have the same behavior for each kind of payment mean. I though about using the Single Table Inheritance but I previously had a bad experience with the combo "STI + associations" and I lost a lot of time.
So, my solution to that problem would be to make an intermediate model PaymentMean that would:
belong to User
be associated to both CreditCard and Bic with a polymorphic "belongs_to" association
as follows:
class User < ActiveRecord::Base
has_many :payment_means
end
class PaymentMean < ActiveRecord::Base
belongs_to :user
belongs_to :payment, polymorphic: true
end
class CreditCard < ActiveRecord::Base
has_one :payment_mean, as: :payment
end
class Bic < ActiveRecord::Base
has_one :payment_mean, as: :payment
end
What do you think about it ? Should I try to use STI (if so, HOW ?) or is this model viable ? Or is there any other solution ?