Rails 5.2 Active Storage add custom attributes

If you need to store additional data with each image and perform queries based on that data, I’d recommend extracting an Image model that wraps an attached file: # app/models/project.rb class Project < ApplicationRecord has_many :images, dependent: :destroy end # app/models/image.rb class Image < ApplicationRecord belongs_to :project has_one_attached :file delegate_missing_to :file scope :positioned, -> { … Read more

How to query records that have an ActiveStorage attachment?

Convention for attachment association names Attachment associations are named using the following convention: <NAME OF ATTACHMENT>_attachment For example, if you have has_one_attached :avatar then the association name will be avatar_attachment. Querying for Active Storage attachments Now that you know how attachment associations are named, you can query them by using joins as you would any … Read more

Disable Active Storage in Rails 5.2

Remove next line from config/application.rb require “active_storage/engine” Remove next line from environments config/environments/*.rb config.active_storage.service = :local Remove next line from app/assets/javascripts/application.js //= require activestorage ActiveStorage rails routes will vanish In case there is statement require ‘rails/all’ in application.rb then you can use solution provided below where you need to require dependency by dependency and to … Read more

How to properly do model testing with ActiveStorage in rails?

I solved using FactoryBot.define do factory :culture do name ‘Soy’ after(:build) do |culture| culture.image.attach(io: File.open(Rails.root.join(‘spec’, ‘factories’, ‘images’, ‘soy.jpeg’)), filename: ‘soy.jpeg’, content_type: ‘image/jpeg’) end end end After describe ‘#image’ do subject { create(:culture).image } it { is_expected.to be_an_instance_of(ActiveStorage::Attached::One) } end

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)