Search code examples
ruby-on-railsbefore-save

Rails :before_save upcase private method


I have two methods in my model which makes changes to the registration field before inserting it in to the DB. The strip_whitespace method works. However, the make_uppercase does not.

I have also tried passing just the make_uppercase method to the before_save callback. Any help would be appreciated.

class Vehicle < ActiveRecord::Base
  belongs_to :vehicle_class
  belongs_to :vehicle_make

  before_save :strip_whitespace, :make_uppercase

  # Strip whitespace from registration field before inserting it in to the DB
  def strip_whitespace
    self.registration.gsub!(/\s+/, '')
  end

  # Make all characters uppercase before inserting it in to the DB
  def make_uppercase
    self.registration.upcase
  end

  private :strip_whitespace, :make_uppercase

end

Solution

  • Try:

    def make_uppercase
      self.registration.upcase!
    end
    

    The bang method (upcase!) modifies the receiver - in this case self.registration.