Reputation: 711
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
Upvotes: 2
Views: 1294
Reputation: 1183
I think you should use upcase!
and not just upcase
or redefine the method like that:
def make_uppercase
self.registration = self.registration.upcase
end
Upvotes: 3
Reputation: 172
Try:
def make_uppercase
self.registration.upcase!
end
The bang method (upcase!) modifies the receiver - in this case self.registration.
Upvotes: 4