Write username in ruby ​​before saving

Is there an easy way to make sure the username is a header before it is saved in ruby ​​.... I was thinking of a model method, but not 100% sure.

therefore, if the user entered apple , then I want it to be saved in db as apple

+4
source share
1 answer

Yes, I think before_save is a good option for this. I don’t know if you had separate fields for the first / last and middle names, this would be easier because the middle name should not be capitalized.

Here is a simple implementation:

 before_save :capitalize_names protected def capitalize_names ['first_name', 'last_name', 'middle_name'].each do |name| self.attributes[name] = self.attributes[name].capitalize end end 
+6
source

Source: https://habr.com/ru/post/1340820/


All Articles