我有一个User
模型、listing
模型和listing_images
模型。
用户型号:
has_many :listings, dependent: :destroy
has_many :listing_images, through: listings
上市模式:
has_many :listing_images, dependent: :destroy
belongs_to :user
Listing_image:
belongs_to :listing
belongs_to :user
当用户删除他们的列表时,我还想删除与该列表相关联的图像。当对列表调用destroy时,listing_images
记录将从数据库中删除,但文件仍在那里。
作为测试,我删除了一个用户,看看它是否会删除配置文件图像,这很好。
我是不是错过了什么?
在listing_image
模型中:
before_destroy :remove_image_file
def remove_image_file
# put logic here to get image path from db record and remove image file
end