When a user submits a form and leaves certain fields blank, they get saved as blank in the DB. I would like to iterate through the params[:user] collection (for example) and if a field is blank, set it to nil before updating attributes. I can’t figure out how to do this though as the only way I know to iterate creates new objects:
coll = params[:user].each do |c|
if c == ""
c = nil
end
end
Thanks.
Consider what you’re doing here by using filters in the controller to affect how a model behaves when saved or updated. I think a much cleaner method would be a
before_savecall back in the model or an observer. This way, you’re getting the same behavior no matter where the change originates from, whether its via a controller, the console or even when running batch processes.Example:
This yields the expected behavior: