I've encountered something really strange, like I see I may be the first one tackling with this. To the basis - I've got a table with all employees, each employee has an NFC id card as id column, their name and now I wanted them to have a "profile picture", something like this.I decided to use Active Storage library, everything works fine with query N + 1, fetching all employees and for each of them fetch separately their profile picture. I don't want to drain my database so I decided to optimise it and here comes the bug I would say.
Here is the N + 1 query:
@employees = Employee.all.map do |employee| { :id => employee.id, :name => employee.name, :avatar => employee.avatar.attached? ? url_for(employee.avatar) : nil } end
The result of the first fetched employee:
{"id": "0029216630","name": "SPECIAL WORKS9","avatar": "http://localhost:3000/rails/active_storage/blobs/redirect/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBDUT09IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--45c6a565c85aede1125f25bacd31d70297362a73/IMG_4629.JPG" }
And this is how it is supposed to work, now let's go ahead to the "optimised" query, yes, with quotation marks 'cause I don't even know whether I'm doing it correctly.
Optimised query
@employees = Employee.with_attached_avatar.all.map do |employee| { :id => employee.id, :name => employee.name, :avatar => employee.avatar.attached? ? url_for(employee.avatar) : nil } end
The result of optimised query
{"id": "0029216630","name": "SPECIAL WORKS9","avatar": null }
Nevertheless, there is attached an image to the record, rails cannot see this image, like there is no any.
The Employee model definition
class Employee < ApplicationRecord has_many :started_occurrences, foreign_key: :start_employee, class_name: "Occurrence" has_many :finished_occurrences, foreign_key: :finish_employee, class_name: "Occurrence" has_one_attached :avatarend
It's an API Rails appGot frontend built upon React.
Any advice?