Thanks for the great library, I find it the best of the Ruby attachments libraries.
I met some strange behavior of my models after including Shrine attachment. There is a short self-contained example of this problem:
# frozen_string_literal: true
require 'bundler/inline'
gemfile(true) do
gem 'activerecord', '5.2.3'
gem 'mysql2'
gem 'fastimage'
gem 'shrine', '2.16.0'
end
require 'active_record'
require 'shrine'
require 'shrine/storage/file_system'
ActiveRecord::Base.connection.create_table(:fake_users) do |table|
table.text :name
table.text :image_data
end unless ActiveRecord::Base.connection.table_exists?(:fake_users)
Shrine.plugin :activerecord
Shrine.storages = {
cache: Shrine::Storage::FileSystem.new(__dir__, prefix: 'assets'),
store: Shrine::Storage::FileSystem.new(__dir__, prefix: 'assets')
}
class Uploader < ::Shrine
end
class FakeUser < ActiveRecord::Base
include Uploader::Attachment.new(:image)
validates :name, length: { maximum: 6 }
end
FakeUser.destroy_all
FakeUser.create(name: 'short', image: File.open('image.jpg'))
user = FakeUser.first
user.save # it's required
user.image = File.open('image.jpg')
FakeUser.transaction { user.save } # transaction is required
It escapes AR validations! But how and why? And it happens only with Shrine attachment included into the model.