views:

38

answers:

2

Hi i have these classes:

class Core < ActiveRecord::Base   

  belongs_to :resource, :polymorphic => true
  belongs_to :image, :class_name => 'Multimedia', :foreign_key => 'image_id'   
end


class Place < ActiveRecord::Base
  has_one :core, :as => :resource       
end

If i try do launch this:

a = Place.find(5)
 a.name ="a"
 a.core.image_id = 24
 a.save

name is saved. image_id no i want save automatically all changes in records in relationship with place class at a.save command. is possible?

thanks

A: 

I think that you can use the build_association method to do that. For example,

a = Place.find(5)
a.name = "a"
a.build_core(:image_id => 24)
a.save

But it might only work if the place object was created before hand.

Corey
+2  A: 

Use :autosave => true

See section titled One-to-many Example for ActiveRecord::AutosaveAssociation.

You'll want something like:

class Place
  has_one :core, :as => :resource, :autosave => true
end

Disclaimer:

The :autosave => true should be used on the "parent" Object. It works great with has_one and has_many, but I've run into great difficulty attempting to use it on a belongs_to. relationship.

macek