Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby on Rails - Can I modify data before it is saved?

Quick example: A user enters a username into a form, and I need to make that text username before storing it in the app's database, thereby making it permanently lowercase.

Where would I put this code, and how would I access the data to be lowercased?

Thanks.

like image 365
SpleenTea Avatar asked Jul 27 '09 08:07

SpleenTea


1 Answers

you should overwrite the attribute writer:

class User < ActiveRecord::Base   def username=(val)     write_attribute(:username, val.downcase)   end end 
like image 118
Michael Siebert Avatar answered Sep 25 '22 13:09

Michael Siebert