275275 t . string :drive_layout
276276 t . string :serial_number
277277 t . integer :person_id
278+ t . references :imageable , polymorphic : true , index : true
278279 t . timestamps null : false
279280 end
280281
@@ -734,6 +735,9 @@ class Picture < ActiveRecord::Base
734735
735736class Vehicle < ActiveRecord ::Base
736737 belongs_to :person
738+ belongs_to :imageable , polymorphic : true
739+ # belongs_to :document, -> { where( pictures: { imageable_type: 'Document' } ) }, foreign_key: 'imageable_id'
740+ # belongs_to :product, -> { where( pictures: { imageable_type: 'Product' } ) }, foreign_key: 'imageable_id'
737741end
738742
739743class Car < Vehicle
@@ -743,13 +747,13 @@ class Boat < Vehicle
743747end
744748
745749class Document < ActiveRecord ::Base
746- has_many :pictures , as : :imageable
750+ has_many :pictures , as : :imageable # polymorphic
747751 belongs_to :author , class_name : 'Person' , foreign_key : 'author_id'
748752 has_one :file_properties , as : :fileable
749753end
750754
751755class Product < ActiveRecord ::Base
752- has_many :pictures , as : :imageable
756+ has_many :pictures , as : :imageable # polymorphic
753757 belongs_to :designer , class_name : 'Person' , foreign_key : 'designer_id'
754758 has_one :file_properties , as : :fileable
755759end
@@ -1336,6 +1340,7 @@ class VehicleResource < JSONAPI::Resource
13361340 immutable
13371341
13381342 has_one :person
1343+ has_one :imageable , polymorphic : true
13391344 attributes :make , :model , :serial_number
13401345end
13411346
@@ -1915,6 +1920,8 @@ class PreferencesResource < PreferencesResource; end
19151920 class SectionResource < SectionResource ; end
19161921 class TagResource < TagResource ; end
19171922 class CommentResource < CommentResource ; end
1923+ class DocumentResource < DocumentResource ; end
1924+ class ProductResource < ProductResource ; end
19181925 class VehicleResource < VehicleResource ; end
19191926 class CarResource < CarResource ; end
19201927 class BoatResource < BoatResource ; end
0 commit comments