How to set a default value?


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


Frank R.
[email protected]

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

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(’ ')

	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 =>

render :action => ‘edit’

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

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, that works.
Anyway to do it in code?

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

Rails mailing list
[email protected]


Frank R.
[email protected]

Hello Frank !

2005/12/6, Frank R. [email protected]:

Anyway to do it in code?

class Account < ActiveRecord::Base
def after_initialize
self.balance = 0.0

See the callbacks section in the Rails API documentation:

Bye !

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


Frank R.
[email protected]