Frage

I have two models

Board
has_one    :pref, :autosave => true,  :dependent => :destroy

Pref

belongs_to :board

The pref object has defaults that are set in the database so no information needs to be used to create the object when the board is created. The ID for the board is in the pref table.

Since the :autosave=> true I thought that when I create and save a new Board object a pref object would be created and saved automatically.

This is not working this way so I must be misunderstanding.

Is there a way to autosave a pref object when a board is saved?

Thank you in advance

War es hilfreich?

Lösung

autosave => true should not create an element for you. The docs say:

If true, always save the associated object or destroy it if marked for destruction, when saving the parent object. If false, never save or destroy the associated object.

You could use a callback to create the pref object when you're creating a new board.

Something along the lines of:

after_create :create_pref

def create_pref
  pref.create!
end
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top