Go to Google Groups Home    Ruby on Rails: Core
Re: [Rails-core] Re: after_initialize/after_find misfeature

Damian Janowski <damian.janow...@gmail.com>

On 7/22/07, Piers Cawley <pdcaw...@bofh.org.uk> wrote:

> def new
>   @customer = Customer.new
> end

> That @customer is never going to be validated, but it does make sense
> for it to have its defaults (which belong in the model and not the
> controller or the view) set correctly so that 'new.rhtml' doesn't need
> any knowledge of any default values when rendering the form.

Really hear you on that one.

If we need to deal with default values in a better way, why not
something like...

class Foo < ActiveRecord::Base
  default :attrib, :to => 1
  default :another_attrib, :to => :another_attrib_defaulter

  protected
  def another_attrib_defaulter
    # ...
  end
end

The second parameter being a hash for some readability and potentially
for some more features...

Thoughts?