Revised: I revised the answer to use before_create and building, not creating, the associated models. The ActiveRecord machinery then takes care of saving the associated models once the parent is saved.
I even tested this code!
# in your Room model...
has_many :doors
before_create :build_main_door
private
def build_main_door
# Build main door instance. Will use default params. One param (:main) is
# set explicitly. The foreign key to the owning Room model is set
doors.build(:main => true)
true # Always return true in callbacks as the normal 'continue' state
end
####### has_one case:
# in your Room model...
has_one :door
before_create :build_main_door
private
def build_main_door
# Build main door instance. Will use default params. One param (:main) is
# set explicitly. The foreign key to the owning Room model is set
build_door(:main => true)
true # Always return true in callbacks as the normal 'continue' state
end
Added...
The build method is added by the owning model's machinery by the has_many statement. Since the example uses has_many :doors (model name Door), the build call is doors.build
See the docs for has_many and has_one to see all of the additional methods that are added.
# If the owning model has
has_many :user_infos # note: use plural form
# then use
user_infos.build(...) # note: use plural form
# If the owning model has
has_one :user_info # note: use singular form
# then use
build_user_info(...) # note: different form of build is added by has_one since
# has_one refers to a single object, not to an
# array-like object (eg user_infos) that can be
# augmented with a build method
Rails 2.x introduced the autosave option for associations. I don't think it applies to the above (I'm using default). Autosave testing results.