0
私は、次のパターンを使用して使用して私のアプリの第二のバージョンを作成しています:バージョン管理と多型団体
class List < ActiveRecord::Base
has_many :listable_items
has_many :lists, through: :listable_items, source: :listable, source_type: 'List'
end
class ListableItem < ActiveRecord::Base
belongs_to :list
belongs_to :listable, polymorphic: true
end
module V2
class List < List
has_many :listable_items
has_many :lists, through: :listable_items, source: :listable, source_type: 'V2::List'
self.inheritance_column = :_non_existing_column
end
end
Module V2
class ListableItem < ListableItem
belongs_to :list, class_name: "V2::List"
belongs_to :listable, polymorphic: true
end
end
list = V2::List.find_by(slug: "people")
=> #<V2::List:0x007fd6e9007f18 id: 97, title: "People"....>
list.listable_items
=> [#<V2::ListableItem:0x007fd6e6497ec8 id: 2633, list_id: 97, listable_id: 100, listable_type: "List",....]
list.listable_items.first.listable
=> #<List:0x007fd6e4185868 id: 100,...>
私はこれが原因ListableItemsのlistable_type列で、クラス定義の起こっていると仮定します。関連するレコードを呼び出すときにdb列に定義されているモデルの代わりに、モデルのV2バージョンを参照する方法がありますか? ListableItemクラスに
def listable_type
"V2::" + super
end
を追加
と呼ばれるリスト可能のクラスを変更しませんでした。
このコードスニペットは、(http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers)[説明を含む]、疑問を解決するかもしれないが、本当に改善するのに役立ちますあなたの投稿の質。将来読者の質問に答えていることを覚えておいてください。そうした人々はあなたのコード提案の理由を知らないかもしれません。 –