Plugins - ActiveRecord Defaults

StarAdd to favorites

Active Record Defaults

If you find this plugin useful, please consider a donation to show your support!

  http://www.paypal.com/cgi-bin/webscr?cmd=_send-money

  Email address: jonathan.viney@gmail.com

Instructions

Allow you to easily specify default values for attributes on new model objects. Eg:

  class Person < ActiveRecord::Base
    defaults :country => 'New Zealand', :type => 'Unknown'

    default :last_name do |person|
      person.first_name
    end
  end

You can also define a defaults method like so:

  class Person < ActiveRecord::Base
    def defaults
      self.first_name = "Jonathan"
    end
  end

The default value is only used if the attribute is not present in the attributes hash:

  p = Person.new
  p.country # "New Zealand"

  p = Person.new(:country => nil)
  p.country # nil

Installation

  script/plugin install http://svn.viney.net.nz/things/rails/plugins/active_record_defaults

Help

Feel free to email with any problems, suggestions, bugs etc…

  jonathan dot viney @ gmail . com

Jonathan Viney

http://svn.viney.net.nz/things/rails/plugins/active_record_defaults/

Rails' (MIT)

  • Currently 4.8/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Model

Tags

Comments

Add a comment
alistair 9 Aug 2007

Nice and neat, thanks!

lambchop 24 Jul 2007

The svn repository appears down as of 2007-07-24.

Nicholas Blumhardt 11 Jun 2007

Thanks! - Much nicer than the alternatives :)

Paul 15 Dec 2006

Just what I was looking for! Cheers!

Jonathan Viney 30 Oct 2006

No, you can only specify static default values with migrations. This plugin allows the default values to be determined when a new model object is created, allowing for features like user-configurable defaults.

> Doesn't ActiveRecord already do this based on the defaults set in the database?

Jonathan Viney 30 Oct 2006

No, you can only specify static default values with migrations. This plugin allows the default values to be determined when a new model object is created, allowing for features like user-configurable defaults.

> Doesn't ActiveRecord already do this based on the defaults set in the database?

Eric Anderson 29 Oct 2006

Doesn't ActiveRecord already do this based on the defaults set in the database?

Search Plugins

Query syntax

Plugins by Category

Sponsors

Rails Kits: Get Code. Get Moving.

Have a comment?