'How do i manage system SystemStackError (stack level too deep): rails API

  1. How i can resolve the system stack too deep?

    Model:

    class Testimony < ApplicationRecord
              belongs_to :user
              before_create :randomize_id
               scope :published, ->{ where(:published => true)}
              has_one_attached :picture
              has_one_attached :video
              #scope :unpublished, where(published: false)
            private 
            def randomize_id
              begin
                self.id = SecureRandom.random_number(1_000_000_000)
              end while Testimony.where(id: self.id).exists?
            end  
    
            end
    
            User Model:
            class User < ApplicationRecord
    
        #attr_accessible :email, :password
            include JsonWebToken
            #searchkick
            #paginates_per 10
            #before_create :create_username
            # before_save  :set_authentication_token
        before_create :randomize_id
        after_save :generate_authentication_token
    
        has_many :articles, dependent: :destroy
        has_many :testimonies, dependent: :destroy
        has_many :services, dependent: :destroy
        has_many :branches, dependent: :destroy
        has_many :songs, dependent: :destroy
        has_many :choirs, dependent: :destroy
        has_many :members, dependent: :destroy
        has_many :lives, dependent: :destroy
        has_many :replies, dependent: :destroy
        has_many :comments, dependent: :destroy
        #has_one_attached :picture
    
    
        This is what i get:
    
         User Load (0.2ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT ?  [["id", 9], ["LIMIT", 1]]
          CACHE User Load (0.0ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT ?  [["id", 9], ["LIMIT", 1]]
        Big Man
        "this user exists [email protected]"
          Testimony Load (0.2ms)  SELECT  "testimonies".* FROM "testimonies" LIMIT ? OFFSET ?  [["LIMIT", 10], ["OFFSET", 0]]
           (0.1ms)  SELECT COUNT(*) FROM "testimonies"
        [active_model_serializers]   ActiveStorage::Attachment Load (0.2ms)  SELECT  "active_storage_attachments".* FROM
    

    "active_storage_attachments" WHERE "active_storage_attachments"."record_id" = ? AND "active_storage_attachments"."record_type" = ? AND "active_storage_attachments"."name" = ? LIMIT ? [["record_id", 936404], ["record_type", "Testimony"], ["name", "video"], ["LIMIT", 1]] [active_model_serializers] Rendered ActiveModel::Serializer::Null with Hash (17.64ms) Completed 500 Internal Server Error in 91ms (ActiveRecord: 2.5ms) SystemStackError (stack level too deep):



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source