Max Williams
2009-Nov-12 12:41 UTC
Problem with has_many :through, :uniq => true with polymorph
Didn''t have quite enough space to describe it there...basically i''m having a problem with the :uniq option in my tags. I''m using acts_as_taggable_on_steroid which adds these associations to my Resource class: Resource has_many :taggings, :as => :taggable, :dependent => :destroy, :include => :tag has_many :tags, :through => :taggings, :uniq => true This uses the join model Tagging: Tagging belongs_to :tag belongs_to :taggable, :polymorphic => true And of course the Tag model: Tag has_many :taggings, :dependent => :destroy has_many_polymorphs :taggables, :from => [:resources, :lessons], :through => :taggings, :dependent => :destroy The uniq option doesn''t seem to stop me from adding the same tag multiple times to a resource:>> resource = Resource.first=> #<TeachingObject id: 59, name: "Clarinet bite point close-up" ...>>> tag = Tag.first=> #<Tag id: 1, name: "recorder">>> resource.tags=> [#<Tag id: 1, name: "recorder">, #<Tag id: 610, name: "chunky chicken">, #<Tag id: 1, name: "recorder">]>> resource.tags << tag=> [#<Tag id: 1, name: "recorder">, #<Tag id: 610, name: "chunky chicken">, #<Tag id: 1, name: "recorder">, #<Tag id: 1, name: "recorder">]>> resource.tags << tag=> [#<Tag id: 1, name: "recorder">, #<Tag id: 610, name: "chunky chicken">, #<Tag id: 1, name: "recorder">, #<Tag id: 1, name: "recorder">, #<Tag id: 1, name: "recorder">] Now, i have other classes which use has_many :through with :uniq => true and they seem fine: for example. resources also join to assets, through a table called ''items'': Resource has_many :items, :dependent => :destroy, :order => :position has_many :assets, :through => :items, :order => :position, :uniq => true Item belongs_to :asset belongs_to :resource Asset has_many :items, :dependent => :destroy, :order => :position has_many :resources, :through => :items, :order => :position Like i say, this behaves as i''d expect:>> resource = Resource.first=> #<TeachingObject id: 59, name: "Clarinet bite point close-up" ... >>> asset = Asset.first=> #<Asset id: 1, name: "07 Clari Clari (notation, perf AI)" ... >>> resource.assets=> []>> resource.assets << asset=> [#<Asset id: 1, name: "07 Clari Clari (notation, perf AI)" ... >]>> resource.assets << asset=> [#<Asset id: 1, name: "07 Clari Clari (notation, perf AI)" ... >] See? It doesn''t add the duplicate. I''m wondering if it''s to do with the polymorphic nature of the tag - resource relationship - maybe the polymorphism is stopping it from noticing that it''s not uniq? or something? Grateful for any help or ideas...i can''t even think of a way to work around this since the problem is buried inside active record''s << method. -- Posted via http://www.ruby-forum.com/.