我正在开发的应用程序有 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
为问题、专业人士和分类法添加了关联。
分类法.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'
知道如何为两者修复它吗?