Forum: Ruby on Rails How to set a default value?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
998baab37c607466125c1235c1ad7006?d=identicon&s=25 Frank Rocco (frocco)
on 2005-12-07 00:49
(Received via mailing list)
Hello,

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



Regards,

Frank Rocco
farocco@hotmail.com
55742d912a9003080d202c5522d4de00?d=identicon&s=25 rails (Guest)
on 2005-12-07 01:30
(Received via mailing list)
If you set the default in the database, ActiveRecord should
automatically
use it.
D6c13e82448f753fde1a51d79e59d92c?d=identicon&s=25 steve (Guest)
on 2005-12-07 01:54
(Received via mailing list)
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
33478bbfbd0e2bf2737eca60a555af76?d=identicon&s=25 farocco (Guest)
on 2005-12-07 02:19
(Received via mailing list)
thanks, that works.
Anyway to do it in code?

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

> Rails mailing list
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails

Regards,

Frank Rocco
farocco@verizon.net
3dd4b52a0946bd698b1d1635a46ea3a3?d=identicon&s=25 francois.beausoleil (Guest)
on 2005-12-07 03:11
(Received via mailing list)
Hello Frank !

2005/12/6, Frank Rocco <farocco@verizon.net>:
> 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/Ca...

Bye !
33478bbfbd0e2bf2737eca60a555af76?d=identicon&s=25 farocco (Guest)
on 2005-12-07 12:09
(Received via mailing list)
On Dec 6, 2005, at 9:11 PM, Francois Beausoleil wrote:

>
> http://lists.rubyonrails.org/mailman/listinfo/rails
Thank you....

Regards,

Frank Rocco
farocco@verizon.net
This topic is locked and can not be replied to.