Getting many to many polymorphic associations to work as advertised

Could anyone please help? Getting exceptions trying to implement:

module P
class List < ActiveRecord::Base
has_many :list_items
has_many :items, :through => :list_items

has_many :contacts, :source => :item, :through => :list_items,
:source_type => “P::Contact”

has_many :users, :source => :item, :through => :list_items,
:source_type => “P::User”

class ListItem < ActiveRecord::Base
belongs_to :list
belongs_to :item, :polymorphic => true

class Contact < ActiveRecord::Base
has_many :lists, :through => :list_items
has_many :items, :as => :item

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


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
    :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 ?


This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs