Rails:通过关联删除多态STI has_many失败

时间:2013-03-28 01:49:51

标签: ruby-on-rails activerecord has-many polymorphic-associations sti

我有一个名为User的模型,它通过Classification模型关联了许多“分类法”。其中一个分类法是名为Topic的模型(继承自分类法)。我的模型User也被称为“可分类”。

编辑:添加了更多模型来澄清问题

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是错误的,它应该是'主题'而不是'分类'。

由于我使用多态关联和STI,我不得不这样配置ActiveRecord:

ActiveRecord::Base.store_base_sti_class = false

然而,它似乎没有在collection.delete上触发。这是rails的错误吗?

0 个答案:

没有答案