【发布时间】:2012-12-31 16:27:16
【问题描述】:
我正在开发的应用程序有 3 个主要模型和许多单表继承模型:
- 问题
- 用户
- 专业
- 代表
- 分类
- 类别
- 主题
- 职业
- 地区
- 地区
- 国家
有多种用户(User、Professional < User、Representant < User),它们都继承自单表继承的User类。
有多种分类法(Category < Taxonomy、Topic < Taxonomy、Profession < Taxonomy、Locality < Taxonomy、Region < Taxonomy、Country < Taxonomy)都继承自具有单表继承的 Taxonomy 类。
问题以及专业人士也通过多对多关系进行分类(他们可以有许多主题、许多专业、许多类别等...)
现在,我正在寻找一种方法来建立这些多态对象之间的多对多关系。我尝试了has_many :through 解决方案并创建了一个分类类。
迁移文件:
class CreateClassifications < ActiveRecord::Migration
def change
create_table :classifications, :id => false do |t|
t.references :classifiable, :null => false, :default => 0, :polymorphic => true
t.references :taxonomy, :null => false, :default => 0, :polymorphic => true
end
add_index :classifications, [:classifiable_id, :taxonomy_id]
add_index :classifications, [:taxonomy_id, :classifiable_id]
end
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
然后我添加了 has_many :through 问题、专业人员和分类法的关联。
Taxonomy.rb
has_many :classifications, :as => :taxonomy, :foreign_key => :taxonomy_id
has_many :classifiables, :through => :classifications, :source => :classifiable
has_many :users, :through => :classifications, :source => :classifiable, :source_type => "User"
has_many :professionals, :through => :classifications, :source => :classifiable, :source_type => "Professional"
has_many :representants, :through => :classifications, :source => :classifiable, :source_type => "Representant"
has_many :questions, :through => :classifications, :source => :classifiable, :source_type => "Question"
has_many :guides, :through => :classifications, :source => :classifiable, :source_type => "Guide"
问题.rb
has_many :classifications, :as => :classifiable, :foreign_key => :classifiable_id, :dependent => :destroy
has_many :taxonomies, :through => :classifications, :source => :taxonomy
has_many :topics, :through => :classifications, :source => :taxonomy, :source_type => "Topic"
专业.rb
has_many :classifications, :as => :classifiable, :foreign_key => :classifiable_id, :dependent => :destroy
has_many :taxonomies, :through => :classifications, :source => :taxonomy
has_many :topics, :through => :classifications, :source => :taxonomy, :source_type => "Topic"
has_many :professions, :through => :classifications, :source => :taxonomy, :source_type => "Profession"
现在,在设置完所有这些之后,事情就不太好了......
我似乎无法将分类法分配给专业人士或问题(即
Question.create(:title => "Lorem Ipsum Dolor Sit Amet", :author => current_user, :topics => [list of topics,...])效果很好,除了未保存的主题。)Where 子句无法正常工作(即
Question.joins(:topics).where(:conditions => {:topics => {:id => [list of topics,...]}})失败并出现no such column: "Topics"."id"错误。
有什么帮助吗?谢谢!
更新
我已经按照说明安装了 gem 'store_base_sti_class'。它对分类模型产生了预期的效果。
#<Classification classifiable_id: 1, classifiable_type: "Professional", taxonomy_id: 17, taxonomy_type: "Topic">
但是,当我查询主题 (Professional.find(1).topics) 时,ActiveRecord 仍在寻找“用户”类而不是“专业”类...
SELECT "taxonomies".* FROM "taxonomies" INNER JOIN "classifications" ON "taxonomies"."id" = "classifications"."taxonomy_id" WHERE "taxonomies"."type" IN ('Topic') AND "classifications"."classifiable_id" = 1 AND "classifications"."classifiable_type" = 'User' AND "classifications"."taxonomy_type" = 'Topic'
知道如何解决这两个问题吗?
【问题讨论】:
-
我的问题可能是我的所有分类类(主题、专业等)中都没有
has_many :through语句吗?如果这是问题所在,则需要一个非常非 DRY 的解决方案......
标签: ruby-on-rails has-many-through polymorphic-associations single-table-inheritance