Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are several issues with each of the available methods, but I believe that defining an <code>after_initialize</code> callback is the way to go for the following reasons:</p> <ol> <li><code>default_scope</code> will initialize values for new models, but then that will become the scope on which you find the model. If you just want to initialize some numbers to 0 then this is <em>not</em> what you want.</li> <li>Defining defaults in your migration also works part of the time... As has already been mentioned this will <em>not</em> work when you just call Model.new.</li> <li>Overriding <code>initialize</code> can work, but don't forget to call <code>super</code>!</li> <li>Using a plugin like phusion's is getting a bit ridiculous. This is ruby, do we really need a plugin just to initialize some default values?</li> <li>Overriding <code>after_initialize</code> <strong>is deprecated</strong> as of Rails 3. When I override <code>after_initialize</code> in rails 3.0.3 I get the following warning in the console:</li> </ol> <blockquote> <p>DEPRECATION WARNING: Base#after_initialize has been deprecated, please use Base.after_initialize :method instead. (called from /Users/me/myapp/app/models/my_model:15)</p> </blockquote> <p>Therefore I'd say write an <code>after_initialize</code> callback, which lets you default attributes <em>in addition to</em> letting you set defaults on associations like so:</p> <pre><code> class Person &lt; ActiveRecord::Base has_one :address after_initialize :init def init self.number ||= 0.0 #will set the default value only if it's nil self.address ||= build_address #let's you set a default association end end </code></pre> <p>Now you have <strong>just one</strong> place to look for initialization of your models. I'm using this method until someone comes up with a better one.</p> <p>Caveats:</p> <ol> <li><p>For boolean fields do:</p> <p><code>self.bool_field = true if self.bool_field.nil?</code></p> <p>See Paul Russell's comment on this answer for more details</p></li> <li><p>If you're only selecting a subset of columns for a model (ie; using <code>select</code> in a query like <code>Person.select(:firstname, :lastname).all</code>) you will get a <code>MissingAttributeError</code> if your <code>init</code> method accesses a column that hasn't been included in the <code>select</code> clause. You can guard against this case like so:</p> <p><code>self.number ||= 0.0 if self.has_attribute? :number</code></p> <p>and for a boolean column...</p> <p><code>self.bool_field = true if (self.has_attribute? :bool_value) &amp;&amp; self.bool_field.nil?</code></p> <p>Also note that the syntax is different prior to Rails 3.2 (see Cliff Darling's comment below)</p></li> </ol>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload