You can:
- set an after_initialize callback that initializes the Bar instances
- set additional :autosave option on the has_many association to assure child Bar instances get saved when saving the parent foo
The code will then look like this:
class Foo < ActiveRecord::Base
has_many :bars, autosave: true
after_initialize :init_bars
def init_bars
# you only wish to add bars on the newly instantiated Foo instances
if new_record?
3.times { bars.build }
end
end
end
You can add the option dependent: :destroy if you wish Bar instances get destroyed when you destroy the parent Foo instance.