Q:

Field becomes blank when before_validation is used in Rails Admin

I am using the Rails Admin gem. When I add a new activity type and create it again with the same name, it validates that name is already taken. But whenever I try to edit one it will give you an error: "name can't be blank"
For example, I created Swimming, and I tried to add a new activity type which is swimming/SWIMMING etc. To avoid this I used the before_validation callback, to make the first letter a capital, then check the uniqueness of name. 
Yes, it's working but whenever I try to edit the name field it will become blank after I submit it.
NOTE: I also tried to use validates :name, presence: true, :uniqueness => {:case_sensitive => true} only without the before_validation but it didn't work.
Activity Type
class ActivityType < ApplicationRecord
  before_destroy :ensure_has_no_activity_type
  before_validation :capitalize_first_letter_name
  has_many :activities

  validates :name, presence: true,:uniqueness => {:case_sensitive => true}, length: { maximum: 20 },format: Utilities::RegexValidations.alphanumeric_underscore
  validates :description, presence: false

  private

  def ensure_has_no_activity_type
    if activities.present?
      errors.add(:base, 'Cannot delete activity type that has activity')
    throw(:abort)
    end
  end
  def capitalize_first_letter_name
    # Capitalize the first letter and the rest will be small letter
    self.name = self.name.capitalize!
  end

end

Question: Why whenever I tried to edit and try to submit it, does the name field become blank? What is the reason for this?

A:

The problem arises from capitalize_first_letter_name. "".capitalize! will return nil. If you change it to "".capitalize that will return blank string as expected.
Moreover, capitalize! will return nil if no changes were made. See https://ruby-doc.org/core-2.2.0/String.html#method-i-capitalize-21.

