Module: ActiveRecord

Defined in:
lib/adva/active_record/has_one_default.rb

Overview

Extends has_one associations to allow for a :default option.

This will make the page.article association always build a default article unless an article is already set or persisted.

class Page < ActiveRecord::Base
  has_one :article, :default => :build_default_article

  def build_default_article
    build_article(:title => 'default title', :body => 'default body')
  end
end

Note that we also include a default scope that includes the associated record to avoid n+1 queries.

Defined Under Namespace

Modules: Associations