Everytime a new record object is created i.e. Record.new, i want certain fields to be the same. Any idea how to do this? Thanks Chris -- Posted via http://www.ruby-forum.com/.
Ashley Moran
2006-Aug-01 11:46 UTC
[Rails] how do set default value for ActiveRecord fields?
On Aug 01, 2006, at 12:26 pm, Chris wrote:> Everytime a new record object is created i.e. Record.new, i want > certain > fields to be the same. > > Any idea how to do this? > Thanks > ChrisChris, You want to set defaults in the database columns. The easiest way to do this is to build your database with Rails migrations: http:// wiki.rubyonrails.org/rails/pages/UnderstandingMigrations and use :default => XXX in your column definitions. Ashley
Kevin Olbrich
2006-Aug-01 11:48 UTC
[Rails] how do set default value for ActiveRecord fields?
On Tuesday, August 01, 2006, at 1:26 PM, Chris wrote:>Everytime a new record object is created i.e. Record.new, i want certain >fields to be the same. > >Any idea how to do this? >Thanks >Chris > >-- >Posted via http://www.ruby-forum.com/. >_______________________________________________ >Rails mailing list >Rails@lists.rubyonrails.org >http://lists.rubyonrails.org/mailman/listinfo/railsEasiest way would be to define :default=>some_value in the migration for each column t.column "title", :string, :default=>''test'' _Kevin www.sciwerks.com -- Posted with http://DevLists.com. Sign up and save your mailbox.
So when you create a new activerecord instance (before you save it) does it look to see what the default value is from the schema? I want the default value to be assigned upon creation of the object, i.e. Record.new, and not when it gets saved. -- Posted via http://www.ruby-forum.com/.
Chris wrote:> So when you create a new activerecord instance (before you save it) does > it look to see what the default value is from the schema? > > I want the default value to be assigned upon creation of the object, > i.e. Record.new, and not when it gets saved.Set the default value in the controller. eg. def firstpage Tablename.column = default_value end I havn''t looked at ruby for a while but its something like that. Does this help? -- Posted via http://www.ruby-forum.com/.
Caio Chassot
2006-Aug-01 13:59 UTC
[Rails] Re: how do set default value for ActiveRecord fields?
On 2006-08-01, at 10:02 , Simon wrote:> Chris wrote: >> So when you create a new activerecord instance (before you save >> it) does >> it look to see what the default value is from the schema? >>It does. That''s also why you can''t have expressions as defaults (eg. NOW()), just literals.>> I want the default value to be assigned upon creation of the object, >> i.e. Record.new, and not when it gets saved.Yep. Rails will look it up, it''ll be set at instantiation.> Set the default value in the controller.Bad idea. Let the model handle it. I''m thinking about writing a plugin that initializes default values. It''s useful when you have more complex defaults, e.g. with inheritance, that each descendant model may have a different default for the same field. Sintax would be something like class Post < AR... defaults :title => ''(untitled)'', :status => ''unpublished'', :whatever => lambda { |record| ... some dynamic magic ... } end You can also do similar just by overriding the accessors.