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.
Frank R. (Guest)
on 2005-12-07 01: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 R.
removed_email_address@domain.invalid
rails (Guest)
on 2005-12-07 02:30
(Received via mailing list)
If you set the default in the database, ActiveRecord should
automatically
use it.
steve (Guest)
on 2005-12-07 02: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
farocco (Guest)
on 2005-12-07 03:19
(Received via mailing list)
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

Regards,

Frank R.
removed_email_address@domain.invalid
francois.beausoleil (Guest)
on 2005-12-07 04:11
(Received via mailing list)
Hello Frank !

2005/12/6, Frank R. <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/Ca...

Bye !
farocco (Guest)
on 2005-12-07 13:09
(Received via mailing list)
On Dec 6, 2005, at 9:11 PM, Francois B. wrote:

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

Regards,

Frank R.
removed_email_address@domain.invalid
This topic is locked and can not be replied to.