Has_many polymorphics problems

I have this situation:

A ‘processable’ model has_many :preprocessors and/or :postprocessors

I do that:

@processor_and_processable.id =

@processor_and_processable.processable_id =

@processor_and_processable.processable_type =

@processor_and_processable.processor_id =

@processor_and_processable.processor_type =

@processor_and_processable.created_at =

@processor_and_processable.updated_at =

@processor_and_processable.lock_version =

@processor_and_processable.created_by =

@processor_and_processable.updated_by =

class ProcessorAndProcessable < ActiveRecord::Base
belongs_to :postprocessor, :foreign_key => :processor_id
belongs_to :preprocessor, :foreign_key => :processor_id
belongs_to :processable, :polymorphic => true
end

@foo.something=

class Foo < ActiveRecord::Base
has_many :processor_and_processables, :as => :processable
has_many :preprocessors, :through => :processor_and_processables
has_many :postprocessors, :through => :processor_and_processables
end

@processor.id =

@processor.descripcion =

@processor.task =

@processor.type =

@processor.created_at =

@processor.updated_at =

@processor.lock_version =

@processor.created_by =

@processor.updated_by =

class Processor < ActiveRecord::Base
end

class Postprocessor < Processor
has_many :processor_and_processables,
:foreign_key => “processor_id”,
:conditions => [‘processor_type =?’,‘Postprocessor’]
has_many :processables, :through => :processor_and_processables
end

class Preprocessor < Processor
has_many :processor_and_processables,
:foreign_key => ‘processor_id’,
:conditions => [‘processor_type =?’,‘Preprocessor’]
has_many :processables, :through => :processor_and_processables
end


…later in the console

foo = Foo.new
foo.postprocessors
[]
foo.preprocessors
[]
foo.postprocessors << Postprocessor.new(:task => “send_mails_to_subscripted_users”)
ActiveRecord::HasManyThroughCantAssociateNewRecords: Cannot associate
new records through ‘Foo#processor_and_processables’ on ‘#’. Both
records must have an id in order to create the has_many :through record
associating them.

Help!
Juan M…

Did you solve this in then end? If you did would you mind sharing as I
am in the same
position.

Any pointers would be very much appreciated :slight_smile:

P

On Jul 17, 12:49 am, Juan M. [email protected]

This one is not the best way. We create the relationship manualy.
I hope that help you.

id :integer(11) not null, primary key

processable_id :integer(11)

processable_type :string(255)

processor_id :integer(11)

pre :boolean(1)

post :boolean(1)

class ProcessorAndProcessable < ActiveRecord::Base
belongs_to :processor
belongs_to :processable, :polymorphic => true
end

id :integer(11) not null, primary key

something :string(255)

class Foo < ActiveRecord::Base
has_many :processor_and_processables, :as => :processable
has_many :preprocessors, :through => :processor_and_processables,
:source => :processor, :conditions => “pre=true and post=false”
has_many :postprocessors, :through => :processor_and_processables,
:source => :processor, :conditions => “post=true and pre=false”
end

@processor.id =

@processor.descripcion =

@processor.task =

class Processor < ActiveRecord::Base
has_many :processor_and_processables
end

Juan M.

You need to save Foo before you can assign the polymorph to it.
Otherwise, has_many_polymorphs doesn’t know which foo_id to assign.