I am trying to implement Active Storage for Ruby on Rails API project. I have placed has_one_attached :picture according to the documentation. And succeeded in uploading of picture on AWS S3 services. Now when I try to access volunteer data it says
ActiveStorage::Attachment Load (0.6ms) SELECT "active_storage_attachments".* FROM "active_storage_attachments" WHERE
"active_storage_attachments"."record_id" = $1 AND "active_storage_attachments"."record_type" = $2 AND "active_storage_attachments"."name" = $3 LIMIT $4 [["record_id", 8695], ["record_type", "Volunteer"], ["name", "picture"], ["LIMIT", 1]] ActiveStorage::Blob Load (0.4ms) SELECT "active_storage_blobs".* FROM "active_storage_blobs" WHERE "active_storage_blobs"."id" = $1 LIMIT $2 [["id", 5], ["LIMIT", 1]] [active_model_serializers] Rendered ActiveModel::Serializer::Null with ActiveStorage::Attached::One (58.41ms) Completed 500 Internal Server Error in 322ms (ActiveRecord: 48.8ms)
SystemStackError (stack level too deep):
I have checked the data through rails console and image is saved in the picture attribute. Schema for volunteer is below
Schema
create_table "volunteers", force: :cascade do |t|
t.string "first_name"
t.string "last_name"
t.string "full_name"
t.string "mobile"
t.string "picture"
t.boolean "kit"
t.boolean "training"
t.boolean "car"
t.boolean "test"
t.bigint "team_id"
t.bigint "education_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.jsonb "attendance", default: []
end
And when I comment out
has_one_attached :picture
in my volunteer model it works and returns me the json object without any error.
How should I solve the Stack Level too deep error?
Updated Answer:
The root cause of this problem in my case was having a field with the same name in my model (column in the table) as the attachment, which is not needed, since Active Storage uses separate tables. And when to_json
is called on such a model object, it causes Stack level too deep error.
After removing the column from the database, the problem goes away.
I see you have the same situation in your model, so i would suggest to you to remove column picture
from the table volunteers
Original Answer:
I just ran into the same problem. For now, i solved it by omitting the attachment from json generation. In your case, it would be something like
@volunteer.to_json(except: :picture)
or using responders
respond_with @volunteer, except: :picture
or
format.json { render json: @volunteer, except: :picture }