# Modifying data before update or save

**URL:** <https://www.ruby-forum.com/t/modifying-data-before-update-or-save/50705>\
**Category:** Rails\
**Created:** [December 20, 2005, 5:50am UTC](https://www.ruby-forum.com/t/modifying-data-before-update-or-save/50705 "2005-12-20T05:50:17Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nic\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/35a633/32.png) [@Nic\_W](https://www.ruby-forum.com/u/Nic_W)\
**Post date:** [December 20, 2005, 5:50am UTC](https://www.ruby-forum.com/t/modifying-data-before-update-or-save/50705/1 "2005-12-20T05:50:17Z")

</div>

Hi,

I’d like to upcase a variable before it is saved, but I’m not sure how  
to access the incoming param or where this work should be performed:

In my update method, I have:

@device = Device.find(params[:id])  
params[:device[macaddress]].upcase \<-- problem code  
if @device.update\_attributes(params[:device])  
…usual stuff here

‘macaddress’ is the variable I’d like to upcase.

Does anybody have suggestions?

- Nic.

---

<div class="post-metadata">

**Author:** ![Nic\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/35a633/32.png) [@Nic\_W](https://www.ruby-forum.com/u/Nic_W)\
**Post date:** [December 20, 2005, 5:56am UTC](https://www.ruby-forum.com/t/modifying-data-before-update-or-save/50705/2 "2005-12-20T05:56:17Z")

</div>

Nic,

In your model, I think you want to hook into the before\_save callback,  
like:

before\_save :normalize

def normalize  
macaddress.upcase!  
#other modifications here  
end

Hope that helps,  
Regards,  
Ed C.

---

<div class="post-metadata">

**Author:** ![Nic\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/35a633/32.png) [@Nic\_W](https://www.ruby-forum.com/u/Nic_W)\
**Post date:** [December 20, 2005, 6:05am UTC](https://www.ruby-forum.com/t/modifying-data-before-update-or-save/50705/3 "2005-12-20T06:05:18Z")

</div>

On Mon, Dec 19, 2005 at 08:48:20PM -0800, Nic W. wrote:

> ‘macaddress’ is the variable I’d like to upcase.
> 
> Does anybody have suggestions?

You could use a before\_save callback in the Device model.

class Device \< ActiveRecord::Base  
before\_save :normalize\_macaddress

# …

private  
def normalize\_macaddress  
self.macaddress = macaddress.upcase if macaddress  
end  
end

marcel
