# Getting many to many polymorphic associations to work as 	advertised...

**URL:** <https://discuss.rubyonrails.org/t/getting-many-to-many-polymorphic-associations-to-work-as-advertised/49917>\
**Category:** rubyonrails-talk\
**Created:** [March 6, 2010, 8:56am UTC](https://discuss.rubyonrails.org/t/getting-many-to-many-polymorphic-associations-to-work-as-advertised/49917 "2010-03-06T08:56:29Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![tribalvibes](https://avatars.discourse-cdn.com/v4/letter/t/34f0e0/32.png) [@tribalvibes](https://discuss.rubyonrails.org/u/tribalvibes)\
**Post date:** [March 6, 2010, 8:56am UTC](https://discuss.rubyonrails.org/t/getting-many-to-many-polymorphic-associations-to-work-as-advertised/49917/1 "2010-03-06T08:56:29Z")

</div>

Could anyone please help? Getting exceptions trying to implement:

module P class List \< ActiveRecord::Base &nbsp;&nbsp;has\_many :list\_items &nbsp;&nbsp;has\_many :items, :through =\> :list\_items

&nbsp;&nbsp;has\_many :contacts, :source =\> :item, :through =\> :list\_items, &nbsp;&nbsp;:source\_type =\> "P::Contact"

&nbsp;&nbsp;has\_many :users, :source =\> :item, :through =\> :list\_items, &nbsp;&nbsp;:source\_type =\> "P::User" end

class ListItem \< ActiveRecord::Base &nbsp;&nbsp;belongs\_to :list &nbsp;&nbsp;belongs\_to :item, :polymorphic =\> true end

class Contact \< ActiveRecord::Base &nbsp;&nbsp;has\_many :lists, :through =\> :list\_items &nbsp;&nbsp;has\_many :items, :as =\> :item end

class User \< ActiveRecord::Base; end # associations like Contact

end

Trying to make List of ListItem where the item is any of Contact, User, etc.

Several problems:

1) P::List.find(:first).contacts does not narrow by the item\_type and instead returns e.g. Contacts with ids from User items. Adding &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:conditions =\> 'item\_type = "P::Contact"' to the :has\_many :contacts fixes this but,

2) P::List.find(:first).items (which should return all items of all item\_types in the first list) instead throws:

ActiveRecord::HasManyThroughAssociationPolymorphicError: Cannot have a has\_many :through association 'P::List#items' on the polymorphic object 'Item#item'.

I've been scratching on this for a while now. Could someone please explain wtf is going on with this setup ?

Thanks, tv
