# Has\_many\_polymorphs and acts\_as\_list?

**URL:** <https://www.ruby-forum.com/t/has-many-polymorphs-and-acts-as-list/120172>\
**Category:** Rails\
**Created:** [November 14, 2007, 1:47pm UTC](https://www.ruby-forum.com/t/has-many-polymorphs-and-acts-as-list/120172 "2007-11-14T13:47:12Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![peportier](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f14d63/32.png) [@peportier](https://www.ruby-forum.com/u/peportier)\
**Post date:** [November 14, 2007, 1:47pm UTC](https://www.ruby-forum.com/t/has-many-polymorphs-and-acts-as-list/120172/1 "2007-11-14T13:47:12Z")

</div>

First, BRAVO for this wonderful plugin: has\_many\_polymorphs ! Now my  
problem:

4 models : Collection, Page, Fragment, Belonging (which is the join  
table):

1 class Collection \<  
ActiveRecord::Base  
2 has\_many\_polymorphs :elements,  
3 :through =\> :belongings,  
4 :from =\> [:pages, :fragments, :collections],  
5 :as =\> :collector,  
6 :parent\_order =\> ‘belongings.position’  
7 end

1 class Page \<  
ActiveRecord::Base  
2 has\_many :fragments  
3 end

1 class Fragment \<  
ActiveRecord::Base  
2 belongs\_to :page  
3 end

1 class Belonging \<  
ActiveRecord::Base  
2 belongs\_to :collector,  
3 :class\_name =\> “Collection”,  
4 :foreign\_key =\> “collector\_id”  
5 belongs\_to :element, :polymorphic =\> true  
6 acts\_as\_list :scope =\> ‘element\_id=#{element\_id} and  
element\_type=#{quote\_value element\_type}’  
7 end

The belongings database table structure:

collector\_id (integer) ; element\_id (integer) ; element\_type  
(string) ; position (integer)

The self referential polymorphic aspect works like a charm!  
But the acts\_as\_list doesn’t!  
Here a demonstration inside the console:

> > c = Collection.create(:name =\> “C1”)  
> > c.elements \<\< Page.create(:name =\> “P1”)  
> > c.elements \<\< Page.create(:name =\> “P2”)  
> > y c  
> > — &id006 !ruby/object:Collection  
> > attributes:  
> > name: C1  
> > updated\_at: &id001 2007-11-14 13:17:45.877143 +01:00  
> > id: 1  
> > created\_at: \*id001  
> > belongings:

- !ruby/object:Belonging  
attributes:  
updated\_at: 2007-11-14 13:18:39  
element\_id: “1”  
id: “1”  
collector\_id: “1”  
element\_type: Page  
position: “1”  
created\_at: 2007-11-14 13:18:39

- !ruby/object:Belonging  
attributes:  
updated\_at: 2007-11-14 13:19:06  
element\_id: “2”  
id: “2”  
collector\_id: “1”  
element\_type: Page  
position: “1”  
created\_at: 2007-11-14 13:19:06  
elements:

- &id003 !ruby/object:Page  
attributes:  
name: P1  
updated\_at: &id002 2007-11-14 13:18:39.397292 +01:00  
id: 1  
created\_at: \*id002  
errors: !ruby/object:ActiveRecord::Errors  
base: \*id003  
errors: {}

- &id005 !ruby/object:Page  
attributes:  
name: P2  
updated\_at: &id004 2007-11-14 13:19:06.052972 +01:00  
id: 2  
created\_at: \*id004  
errors: !ruby/object:ActiveRecord::Errors  
base: \*id005  
errors: {}

new\_record: false  
new\_record\_before\_save: true  
=\> nil

> > c.elements.first.move\_to\_bottom  
> > NoMethodError: undefined method `move_to_bottom' for #<Page:0x3318ba4> from /opt/local/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/ active_record/base.rb:1860:in`method\_missing’  
> > from (irb):8  
> > from :0

I would be very pleased if you could help me …

---

<div class="post-metadata">

**Author:** ![peportier](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f14d63/32.png) [@peportier](https://www.ruby-forum.com/u/peportier)\
**Post date:** [November 15, 2007, 2:07pm UTC](https://www.ruby-forum.com/t/has-many-polymorphs-and-acts-as-list/120172/2 "2007-11-15T14:07:44Z")

</div>

Just for completness …  
In fact I have to refer to the belongings table when interested by the  
acts\_as\_list behavior. So:  
c.belongings.first.move\_to\_bottom  
And not:  
c.elements.first.move\_to\_bottom  
etc. etc.

And in my case the scope I meant wasn’t:  
element\_id=#{element\_id} and element\_type=#{quote\_value  
element\_type}’  
But:  
collector\_id  
Since I want the elements inside a collection to act as a list.

On 14 nov, 13:46, peportier [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
