# How to set a default value?

**URL:** <https://www.ruby-forum.com/t/how-to-set-a-default-value/62345>\
**Category:** Rails\
**Created:** [December 7, 2005, 12:49am UTC](https://www.ruby-forum.com/t/how-to-set-a-default-value/62345 "2005-12-07T00:49:55Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![frocco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/a87d85/32.png) [@frocco](https://www.ruby-forum.com/u/frocco)\
**Post date:** [December 7, 2005, 12:49am UTC](https://www.ruby-forum.com/t/how-to-set-a-default-value/62345/1 "2005-12-07T00:49:55Z")

</div>

Hello,

I have a price field that I validate of numbers.  
How can I default a 0.0 value?

Regards,

Frank R.  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![frocco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/a87d85/32.png) [@frocco](https://www.ruby-forum.com/u/frocco)\
**Post date:** [December 7, 2005, 1:30am UTC](https://www.ruby-forum.com/t/how-to-set-a-default-value/62345/2 "2005-12-07T01:30:31Z")

</div>

If you set the default in the database, ActiveRecord should  
automatically  
use it.

---

<div class="post-metadata">

**Author:** ![frocco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/a87d85/32.png) [@frocco](https://www.ruby-forum.com/u/frocco)\
**Post date:** [December 7, 2005, 1:54am UTC](https://www.ruby-forum.com/t/how-to-set-a-default-value/62345/3 "2005-12-07T01:54:51Z")

</div>

i’m developing a simple content management app in rails where users can  
create content in the browser. i’d like to give them the ability to  
“tag” content with multiple keywords. so on the form where they are  
creating/editing content there is a text field called tag\_tags[] where  
they can type in space-seperated tags.

i have 3 tables: pages, tags, and pages\_tags. i pre-populated tags with  
3 records just to test things out.

if a user enters a pre-existing tag in the field, a record gets added to  
the pages\_tags table appropriately. however, if the user enters a tag  
that doesn’t already exist, i’d like to add a record to tags, and then  
add a record to pages\_tags with the new tag\_id.  
here’s my update method in pages\_controller.rb

* * *

def update  
@page = Page.find(params[:id])  
a = @params[:tag\_tags].to\_s  
ta = a.split(’ ')

```
ta.each{|t|
	tg = Tag.find(:all, :conditions => ["tag = ?", t])
	@page.tags << tg
}

 if @page.update_attributes(params[:page])
   flash[:notice] = 'Page was successfully updated.'
   redirect_to :action => 'list', :id => @page, :section_id =>

```

@page.section\_id  
else  
render :action =\> ‘edit’  
end  
end

* * *

i’ve tried doing a  
if tg.nil?  
@nt = Tag.create(t)  
@nt.save  
@page.tags \<\< nt  
end

but that doesn’t add a record to the tags table.

any ideas? i am only a few weeks into rails so i am probably missing  
something obvious!  
thanks!  
steve

---

<div class="post-metadata">

**Author:** ![frocco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/a87d85/32.png) [@frocco](https://www.ruby-forum.com/u/frocco)\
**Post date:** [December 7, 2005, 2:19am UTC](https://www.ruby-forum.com/t/how-to-set-a-default-value/62345/4 "2005-12-07T02:19:09Z")

</div>

thanks, that works.  
Anyway to do it in code?

On Dec 6, 2005, at 7:28 PM, Ken B. wrote:

> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

Regards,

Frank R.  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![frocco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/a87d85/32.png) [@frocco](https://www.ruby-forum.com/u/frocco)\
**Post date:** [December 7, 2005, 3:11am UTC](https://www.ruby-forum.com/t/how-to-set-a-default-value/62345/5 "2005-12-07T03:11:59Z")

</div>

Hello Frank !

2005/12/6, Frank R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Anyway to do it in code?

class Account \< ActiveRecord::Base  
def after\_initialize  
self.balance = 0.0  
end  
end

See the callbacks section in the Rails API documentation:  
[http://api.rubyonrails.com/classes/ActiveRecord/Callbacks.html](http://api.rubyonrails.com/classes/ActiveRecord/Callbacks.html)

Bye !

---

<div class="post-metadata">

**Author:** ![frocco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/a87d85/32.png) [@frocco](https://www.ruby-forum.com/u/frocco)\
**Post date:** [December 7, 2005, 12:09pm UTC](https://www.ruby-forum.com/t/how-to-set-a-default-value/62345/6 "2005-12-07T12:09:07Z")

</div>

On Dec 6, 2005, at 9:11 PM, Francois B. wrote:

> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)  
> Thank you…

Regards,

Frank R.  
removed\_email\_address@domain.invalid
