我有一段关系,我很难建模。
我有一个Subscription
类,它是一个常规的ActiveRecord模型,它可以有一个或多个PaymentSources
。但问题是,付款来源可能会引用CreditCard
或BankAccount
。
鉴于这些模型与它们相关的数据非常不同,我觉得STI不是一个很好的选择。所以我想知道在Rails中是否存在已建立或推荐的方法,其中模型has_many是另一个模型,实际上是2个或更多类的抽象,它们不共享相同的数据布局。
理想情况下,在这个特定示例中,我可以说subscription.payment_source.default
之类的内容,并将其引用CreditCard
或BankAccount
,具体取决于用户选择的首选结算方式
答案 0 :(得分:4)
[更新]经过一番思考后,我会做选项2(更完整的解决方案),这是一个具有前瞻性的灵活性,但如果你不需要所有这些复杂性,我只做选项1。
class Subscription < ApplicationRecord
belongs_to :credit_card
belongs_to :bank_account
def payment_sources
[credit_card, bank_account].compact
end
def default_payment_source
case user.preferred_billing_method # assuming you have an integer column in users table called `preferred_billing_method`
when 0 then credit_card # asssuming 0 means "Credit Card"
when 1 then bank_account # assuming 1 means "Bank Account"
else NotImplementedError
end
end
end
Subscription.first.default_payment_source
# => returns either `CreditCard` or `BankAccount`, or `nil`
Subscription.first.payment_sources.first
# => returns either `CreditCard` or `BankAccount`, or `nil`
class User < ApplicationRecord
belongs_to :default_payment_source, class_name: 'PaymentSource'
has_many :subscriptions
end
class Subscription < ApplicationRecord
belongs_to :user
has_many :payment_sources_subscriptions
has_many :payment_sources, through: :payment_sources_subscriptions
end
# This is just a join-model
class PaymentSourcesSubscription < ApplicationRecord
belongs_to :subscription
belongs_to :payment_source
validates :subscription, uniqueness: { scope: :payment_source }
end
# this is your "abstract" model for "payment sources"
class PaymentSource < ApplicationRecord
belongs_to :payment_sourceable, polymorphic: true
has_many :payment_sources_subscriptions
has_many :subscriptions, through: :payment_sources_subscriptions
validates :payment_sourceable, uniqueness: true
end
class CreditCard < ApplicationRecord
has_one :payment_source, as: :payment_sourceable
end
class BankAccount < ApplicationRecord
has_one :payment_source, as: :payment_sourceable
end
User.first.default_payment_source.payment_sourceable
# => returns either `CreditCard` or `BankAccount`, or `nil`
Subscription.first.payment_sources.first.payment_sourceable
# => returns either `CreditCard` or `BankAccount`, or `nil`