Initializing object graphs/associations

Say I have these two classes:

class Client < ActiveRecord::Base
has_one :setting, :dependent => :destroy
end

class Setting < ActiveRecord::Base
belongs_to :client
end

Is there any way to write an init method that will create and assign
the client a setting before evaluating params, but otherwise works ‘as
normal’?

Thanks,
Isak

Does this work for you:

class Client < ActiveRecord::Base
has_one :setting, :dependent => :destroy
after_create{ setting = Setting.create() }
end

This will create an associated setting when the Client is first saved
(inserted) into the database.

Cheers,
Max

Max M. wrote:

Does this work for you:

class Client < ActiveRecord::Base
has_one :setting, :dependent => :destroy
after_create{ setting = Setting.create() }
end

This will create an associated setting when the Client is first saved
(inserted) into the database.

Cheers,
Max

Try this instead

class Client < ActiveRecord::Base
has_one :setting, :dependent => :destroy
after_create :create_setting

private
  def create_setting
    self.setting = Setting.create
  end

end