# Has\_many polymorphics problems

**URL:** <https://www.ruby-forum.com/t/has-many-polymorphics-problems/225956>\
**Category:** Rails\
**Created:** [July 17, 2007, 1:49am UTC](https://www.ruby-forum.com/t/has-many-polymorphics-problems/225956 "2007-07-17T01:49:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![juan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a698b9/32.png) [@juan](https://www.ruby-forum.com/u/juan)\
**Post date:** [July 17, 2007, 1:49am UTC](https://www.ruby-forum.com/t/has-many-polymorphics-problems/225956/1 "2007-07-17T01:49:55Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![juan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a698b9/32.png) [@juan](https://www.ruby-forum.com/u/juan)\
**Post date:** [August 3, 2007, 3:58pm UTC](https://www.ruby-forum.com/t/has-many-polymorphics-problems/225956/2 "2007-08-03T15:58:15Z")

</div>

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 🙂

P

On Jul 17, 12:49 am, Juan M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![juan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a698b9/32.png) [@juan](https://www.ruby-forum.com/u/juan)\
**Post date:** [August 27, 2007, 3:44pm UTC](https://www.ruby-forum.com/t/has-many-polymorphics-problems/225956/3 "2007-08-27T15:44:18Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![juan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a698b9/32.png) [@juan](https://www.ruby-forum.com/u/juan)\
**Post date:** [August 27, 2007, 7:39pm UTC](https://www.ruby-forum.com/t/has-many-polymorphics-problems/225956/4 "2007-08-27T19:39:05Z")

</div>

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.
