Automatic creation of a has_one associated object when it is first accessed.
class User < ActiveRecord::Base has_one :profile, :autocreate => true end class Profile < ActiveRecord::Base belongs_to :user end # make a single user >> user = User.create(:name => 'Dr. User') => #<User id: 1, name: "Dr. User"> # look for any profile objects >> Profile.count => 0 # access the user's profile object to see it automatically created >> user.profile => #<Profile id: 1, title: nil, user_id: 1>
As a Rails plugin.
./script/plugin install git://github.com/jqr/has_one_autocreate.git
Prefer gems? Add this to your environment.rb and run the following command.
config.gem 'has_one_autocreate' $ rake gems:install
rdoc.info/projects/jqr/has_one_autocreate
- Homepage
- License
-
Copyright © 2008 Elijah Miller <[email protected]>, released under the MIT license.