# Add default value on Insert

**URL:** <https://www.ruby-forum.com/t/add-default-value-on-insert/66038>\
**Category:** Rails\
**Created:** [August 22, 2006, 11:17pm UTC](https://www.ruby-forum.com/t/add-default-value-on-insert/66038 "2006-08-22T23:17:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jimweirich](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ce73a5/32.png) [@jimweirich](https://www.ruby-forum.com/u/jimweirich)\
**Post date:** [August 22, 2006, 11:17pm UTC](https://www.ruby-forum.com/t/add-default-value-on-insert/66038/1 "2006-08-22T23:17:55Z")

</div>

The following inserts a new product, no problem. Howeve, I have to  
prefix  
the filename with "\images" so upon retrieval it will looki in the  
right  
directory.

How can I modify this code to append "\images" to the filename column?

def create  
@product = Product.new(@params[:product])  
if @product.save  
flash[‘notice’] = ‘Product was successfully created.’  
end  
end

* * *

Check the weather nationwide with MSN Search: Try it now!

> **[weather - Bing](https://www.bing.com/search?q=weather&FORM=WLMTAG)**
>
> Intelligent search from Bing makes it easier to quickly find what you’re looking for and rewards you.

---

<div class="post-metadata">

**Author:** ![jimweirich](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ce73a5/32.png) [@jimweirich](https://www.ruby-forum.com/u/jimweirich)\
**Post date:** [August 22, 2006, 11:39pm UTC](https://www.ruby-forum.com/t/add-default-value-on-insert/66038/2 "2006-08-22T23:39:57Z")

</div>

You can define a before\_save callback in your model to call a function  
which  
will prepend “\images”.

---

<div class="post-metadata">

**Author:** ![jimweirich](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ce73a5/32.png) [@jimweirich](https://www.ruby-forum.com/u/jimweirich)\
**Post date:** [August 22, 2006, 11:41pm UTC](https://www.ruby-forum.com/t/add-default-value-on-insert/66038/3 "2006-08-22T23:41:58Z")

</div>

On 8/22/06, Jim W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> The following inserts a new product, no problem. Howeve, I have to prefix  
> the filename with "\images" so upon retrieval it will looki in the right  
> directory.
> 
> How can I modify this code to append "\images" to the filename column?

def create  
@product = Product.new(@params[:product])  
@product.filename = “\images\” + @product.filename  
if @product.save  
flash[‘notice’] = ‘Product was successfully created.’  
end  
end

---

<div class="post-metadata">

**Author:** ![jimweirich](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ce73a5/32.png) [@jimweirich](https://www.ruby-forum.com/u/jimweirich)\
**Post date:** [August 23, 2006, 12:18am UTC](https://www.ruby-forum.com/t/add-default-value-on-insert/66038/4 "2006-08-23T00:18:02Z")

</div>

This approach might be problematic if you’re creating new products in  
other  
actions or elsewhere in your rails app. Using before\_save won’t pose the  
same  
problem.
