# Add value to params before save

**URL:** <https://www.ruby-forum.com/t/add-value-to-params-before-save/78937>\
**Category:** Rails\
**Created:** [December 10, 2006, 9:20pm UTC](https://www.ruby-forum.com/t/add-value-to-params-before-save/78937 "2006-12-10T21:20:21Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![paulbutcher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a5b964/32.png) [@paulbutcher](https://www.ruby-forum.com/u/paulbutcher)\
**Post date:** [December 10, 2006, 9:20pm UTC](https://www.ruby-forum.com/t/add-value-to-params-before-save/78937/1 "2006-12-10T21:20:21Z")

</div>

How do I add a value to params coming from a form before it is saved? I  
have a value passed in the params which I use to do a find in a related  
table. All works fine. But when I want to add that into the current save  
â€“ current table - I’m lost.

---

<div class="post-metadata">

**Author:** ![paulbutcher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a5b964/32.png) [@paulbutcher](https://www.ruby-forum.com/u/paulbutcher)\
**Post date:** [December 10, 2006, 11:20pm UTC](https://www.ruby-forum.com/t/add-value-to-params-before-save/78937/2 "2006-12-10T23:20:14Z")

</div>

I’m not entirely clear what you are trying to achieve but why not set  
the attribute directly then save? You don’t need to add it to the  
params hash, but just to the object itself (if I understand your  
question correctly).

@my\_object.attribute = value  
if @my\_object.save

# do something here

else

# do something else

end

You might want to also check out the ‘update’ and ‘update\_attributes’  
methods as well.

---

<div class="post-metadata">

**Author:** ![paulbutcher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a5b964/32.png) [@paulbutcher](https://www.ruby-forum.com/u/paulbutcher)\
**Post date:** [December 10, 2006, 11:26pm UTC](https://www.ruby-forum.com/t/add-value-to-params-before-save/78937/3 "2006-12-10T23:26:15Z")

</div>

…and the ‘update\_attribute’ method as well.

update\_attribute and update\_attributes allow you to set the  
attribute(s) and save the record in one step. So you might be using  
the params hash to populate the bulk of the object’s attributes and  
then adding in extra attribute(s) with these methods.

---

<div class="post-metadata">

**Author:** ![paulbutcher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a5b964/32.png) [@paulbutcher](https://www.ruby-forum.com/u/paulbutcher)\
**Post date:** [December 11, 2006, 12:46am UTC](https://www.ruby-forum.com/t/add-value-to-params-before-save/78937/4 "2006-12-11T00:46:40Z")

</div>

unknown wrote:

> …and the ‘update\_attribute’ method as well.
> 
> update\_attribute and update\_attributes allow you to set the  
> attribute(s) and save the record in one step. So you might be using  
> the params hash to populate the bulk of the object’s attributes and  
> then adding in extra attribute(s) with these methods.

Attribute! Thanks. 🙂
