Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

has_one belongs_to association autosave => true not saving

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

like image 667
chell Avatar asked Nov 29 '25 15:11

chell


1 Answers

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
like image 63
polarblau Avatar answered Dec 01 '25 08:12

polarblau



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!