title: Stack level too deep on user.save
body: <p>I want to assign a confirmation code to my users while creating one. And I also titleize some columns before saving-updating them. So my user.rb looks like this (it may be a bit messy):</p>
  
  <pre><code>// user.rb
  *** some code ***
  before_save { titleize_column(:name)
                  titleize_column(:surname)
                  capitalize_column(:complaints) 
                  capitalize_column(:education)
                  capitalize_column(:job)
                  capitalize_column(:complaintsdetails)
                  capitalize_column(:prediagnosis)
                  capitalize_column(:existingdiagnosis)
                  capitalize_column(:knownilnessesother)
                  capitalize_column(:usedmedicine)
                  capitalize_column(:operation)
                  capitalize_column(:trauma)
                  capitalize_column(:allergy)
                  capitalize_column(:otherhabits)
                  capitalize_column(:motherother)
                  capitalize_column(:fatherother)
                  capitalize_column(:siblingsother)
                }
    before_save :generate_confirmation_code
  protected
      def generate_confirmation_code
        unless self[:confirmed]
          if(self[:type] == 'Patient')
            update_attribute :confirmation_code, SecureRandom.urlsafe_base64(20)
            update_attribute :confirmed, false
          else
            update_attribute :confirmed, true
          end
        end
      end
  
  protected
      def capitalize_column(attr)
        unless self[attr].nil?
          self[attr] = Unicode::capitalize self[attr]
        end
      end
  
    protected
      def titleize_column(attr)
        unless self[attr].nil?
          words = self[attr].split
          words.each_with_index do |v,i|
            words[i] = Unicode::capitalize v
          end
          self[attr] = words.join(" ")
        end
      end
  </code></pre>
  
  <p>I'm using separate methods for titleizing and capitalizing columns because they may be nil when first creating a user, so I'm checking if it is null or not in those methods. This structure works fine on a normal signup with strong parameters. However, if I try to use twitter signup with the method below, it gives me the error 'stack level too deep' and I can see that it calls the <code>generate_confirmation_code</code> 123 times from the application trace and then these happens:</p>
  
  <p><strong>app/models/user.rb:83:in <code>each'
  app/models/user.rb:83:in</code>each_with_index'
  app/models/user.rb:83:in <code>titleize_column'
  app/models/user.rb:20:in</code>block in '
  app/models/user.rb:64:in <code>generate_confirmation_code' (x123 times)
  app/models/user.rb:101:in</code>from_omniauth'
  app/controllers/socials_controller.rb:4:in `create'</strong></p>
  
  <pre><code>// method for signing up/logging in a user from twitter
  
    class &lt;&lt; self
      def from_omniauth(auth_hash)
        if exists?(uid: auth_hash['uid'])
          user = find_by(uid: auth_hash['uid'])
        else
          user = find_or_create_by(uid: auth_hash['uid'], provider: auth_hash['provider'], type: 'Patient')
          user.password_digest = User.digest('111111')
          user.name = auth_hash['info']['name']
          user.location = get_social_location_for user.provider, auth_hash['info']['location']
          user.avatar = auth_hash['info']['image']
          user.url = get_social_url_for user.provider, auth_hash['info']['urls']
          user.save!            // THIS IS THE LINE 101!
          conversation = Conversation.create()
          user.conversation = conversation
          admin = Admin.first
          admin.conversations &lt;&lt; conversation
          user.progress = Progress.create(active_state:1)
        end
        user
      end
  </code></pre>
  
  <p>I think I'm messing up by using before_save not properly, but do not know how to do it right. What am I doing wrong here?</p>