Rails conditional subclass STI from base class

I am developing a project in which I have an object that can have two types of assets: Pictures and Videos, basically.

Since I want all assets to be in the same table and one upload form for photos or videos, I use Single Table Inheritance, in which both images and videos go down from the Asset class. In addition, I will use different checks / callbacks depending on whether this is a video or a picture.

I use paperclip to work with the download process, and my idea is to download the file and create an Asset with it, the application will create an appropriate subclass (image or video) depending on the mime type of the downloaded file.

This is a sketch of my classes:

class Project < ActiveRecord::Base
  has_many :assets
  accepts_nested_attributes_for :assets
end

class Asset < ActiveRecord::Base
  belongs_to :project
  has_uploaded_file :content, ...
end

class Picture < Asset
  validate :image_size
  ...
end

class Video < Asset
  after_save :convert_format
  ...
end

- before_save Asset , , .

?

+3
1

, . , , Asset, , ( , API ).

+1

Source: https://habr.com/ru/post/1781346/


All Articles