我有一个名为 User 的模型,它有许多通过分类模型关联的“分类法”。其中一种分类法是称为 Topic 的模型(继承自分类法)。我的模型用户也称为“可分类”。
编辑:添加了更多模型来澄清问题
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
除非我想删除关联,否则一切正常。
user = User.find(12) # any user
topic = user.topics.last # any of his topics
user.topics.delete(topic)
SQL ActiveRecord 运行如下:
DELETE FROM "classifications" WHERE "classifications"."classifiable_id" = 12 AND "classifications"."classifiable_type" = 'User' AND "classifications"."taxonomy_id" = 34 AND "classifications"."taxonomy_type" = 'Taxonomy'
显然,taxonomy_type 是错误的,它应该是“Topic”而不是“Taxonomy”。
由于我使用的是多态关联和 STI,因此我必须这样配置 ActiveRecord:
ActiveRecord::Base.store_base_sti_class = false
但是,它似乎不会在 collection.delete 上触发。这是rails的错误吗?