Ruby on Rails: before_save fields to lowercase

String#downcase does not mutate the string, it simply returns a modified copy of that string. As others said, you could use the downcase! method.

def downcase_fields
  name.downcase!
end

However, if you wanted to stick with the downcase method, then you could do the following:

def downcase_fields
  self.name = name.downcase
end

This reassigns the name instance variable to the result of calling downcase on the original value of name.


downcase returns a copy of the string, doesn't modify the string itself. Use downcase! instead:

def downcase_fields
  self.name.downcase!
end

See documentation for more details.


You're not setting name to downcase by running self.name.downcase, because #downcase does not modify the string, it returns it. You should use the bang downcase method

self.name.downcase!

However, there's another way I like to do it in the model:

before_save { name.downcase! }