0

I have a model called User which has many "taxonomies" associated through a Classification model. One of these taxonomies is a model called Topic (inheriting from Taxonomy). My model User is also called a "classifiable".

EDIT: Added more models to clarify the problem

class User < ActiveRecord::Base
  has_many :classifications, :as => :classifiable, :foreign_key => :classifiable_id
  has_many :topics, :through => :classifications, :source => :taxonomy, :source_type => "Topic"
end

class Taxonomy < ActiveRecord::Base
end

class Topic < Taxonomy
  has_many :classifications, :as => :taxonomy, :foreign_key => :taxonomy_id, :source_type => "Topic"
  has_many :professionals, :through => :classifications, :source => :classifiable, :source_type => "User", :conditions => {:is_a_professional => true}
  has_many :questions,     :through => :classifications, :source => :classifiable, :source_type => "Question"
  has_many :guides,        :through => :classifications, :source => :classifiable, :source_type => "Guide"
end

class Classification < ActiveRecord::Base
  attr_accessible :classifiable, :classifiable_id, :classifiable_type,
                  :taxonomy, :taxonomy_id, :taxonomy_type

  belongs_to :classifiable, :polymorphic => true
  belongs_to :taxonomy,     :polymorphic => true
end

Everything works well except when I want to delete an association.

user = User.find(12) # any user
topic = user.topics.last # any of his topics
user.topics.delete(topic)

The SQL ActiveRecord runs is the following:

DELETE FROM "classifications" WHERE "classifications"."classifiable_id" = 12 AND "classifications"."classifiable_type" = 'User' AND "classifications"."taxonomy_id" = 34 AND "classifications"."taxonomy_type" = 'Taxonomy'

Clearly, the taxonomy_type is wrong, it should be 'Topic' and not 'Taxonomy'.

Since I am using polymorphic associations and STI, I had to config ActiveRecord as such:

ActiveRecord::Base.store_base_sti_class = false

However, it does not seem to trigger on collection.delete. Is this a bug with rails?

Jonathan Roy
  • 903
  • 11
  • 20

0 Answers0