私は以下のレールモデルを持っています。継承を使用して多形関係を取得するには?
class Service < ApplicationRecord
self.table_name ='service'
end
class ChildA < Service
has_many :charges
end
class ChildB < Service
self.table_name = "childb_table"
has_many :charges
end
class Charges < ApplicationRecord
:belongs_to chargeable, polymorphic: true
end
# code using this
a = ClassB.new.save
charge = Charges.new.save
a.charges.add(a) # this adds the column with the class name 'Service'
私は、オブジェクトを保存しようとしたがchargeable_type
フィールドは常に基本クラスservice
、決して子クラスとして設定されています。
どうすればこの問題を回避できますか?
は 'べきではない:charges':has_manyのをcharges'ラインは' has_manyのこと? ( 'belongs_to'行と同じです)また、ここで何が起こっているのか全く分かりません。どのオブジェクトを保存しようとしていますか?あなたはオブジェクトを保存するために使用しているコードを表示できますか?私はここにペーストされたものでは確信が持てませんが、あなたはSTIを探しているかもしれません(https://eewang.github.io/blog/2013/03/12/how-and-when-to-use-single-table -inheritance-in-rails /)を使用します。 – Glyoko