title: ActiveRecord default value if attribute is nil
body: <p>Looking for a cleaner way to set a default value if attribute is not set yet or has been deleted, and returns nil.</p>
  
  <pre><code>class Category &lt; ActiveRecord::Base
    has_and_belongs_to_many :restaurants
    belongs_to :picture
  
    def set_picture
      if self.picture.nil?
        Picture.default_pic
      else
        self.picture
      end
    end
  end
  
  class Picture &lt; ActiveRecord::Base
    belongs_to :review
  
    def self.default_pic
      Picture.new(url: "/assets/default.jpg")
    end
  end
  
  # index.html.erb
  &lt;%= image_tag category.set_picture.url %&gt;
  </code></pre>
  
  <p>categories has many restaurants, and restaurants has many reviews. Reviews has one to one picture. category should be allowed to select from one of its associated pictures, or defaults to image in assets folder.</p>
  
  <p>The #set_picture needs to get refactored out. Hopefully to a callback of some type:</p>
  
  <pre><code>class Category &lt; ActiveRecord::Base
    belongs_to :picture, defaults_to: Picture.default_pic
  end
  </code></pre>
  
  <p>Is there a callback that does the above? Can I create one? Or is my framework wrong?</p>