过去,用户可以自由上传横幅而无需验证。就在最近,我们推出了横幅验证。我们如何更新具有OLD横幅的用户记录?
class User < ActiveRecord::Base
require 'carrierwave/orm/activerecord'
mount_uploader :banner, BannerUploader
validate :banner_size_validation, :if => :banner?
validate :check_banner_dimensions, :if => :banner?
def banner_size_validation
errors.add :banner, 'should be less than 1mb' if banner.size > 1.megabytes
end
def check_banner_dimensions
tmp_banner = MiniMagick::Image.open(banner)
errors.add :banner, 'height must be no more than 400px.' if tmp_banner[:height] > 400
end
end
例如。 ID 3的用户在未强制执行验证时上传了横幅。
User.find(3).update_attributes!(:artist_name => 'vanilla')
# ActiveRecord::RecordInvalid: Validation failed: Banner height must be no more than 400px.
删除横幅可能会令用户失望。调整现有横幅的大小会使一些横幅看起来“有弹性”。有什么建议吗?