Featured post
paperclip - Rails STI conditional sub-classing from base class -
i'm developing project have entity may have 2 kinds of assets: pictures , videos, basically.
since want assets on same table , single upload form either pictures or videos, i'm using single table inheritance having both picture , video descending asset class. also, i'll running different validations/callbacks depending on whether video or picture.
i'm using paperclip deal upload process, , idea when uploading file , creating asset it, application instantiate correct subclass (picture or video) depending on mime-type of uploaded file.
this sketch of 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
my idea implement before_save
callback on asset class , try instantiate correct class there, i'm not sure how or if it's idea.
any idea this?
while should favor fat models , skinny controllers, me seems better placed in controller. primary rationale doing in asset
model coupling base type subtypes, doesn't feel right me (although see apis time).
- Get link
- X
- Other Apps
Comments
Post a Comment