我开发的应用主要有3种型号和许多单表继承机型:的Rails:类继承和多态复杂的has_many:通过关联
- 问题
- 用户
- 专业
- 代理人
- 分类标准
- 类别
- 主题
- 职业
- 局部性
- 地区
- 国家
有多种类型的用户(User
,Professional < User
,Representant < User
)所有继承具有单个表继承的User类。
有多种类型的分类法(Category < Taxonomy
,Topic < Taxonomy
,Profession < Taxonomy
,Locality < Taxonomy
,Region < Taxonomy
,Country < Taxonomy
)其中所有从与单表继承的分类类继承的。
问题,以及专业人士也正在通过分类多对多关系(他们可以有很多话题,很多行业,很多类,等...)
现在,我正在寻找一种方式建立这些多态对象之间的多对多关系。我试过has_many :through
解决方案并创建了一个Classification类。
迁移文件:
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"
Question.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"
Professional.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,...])
的效果很好。)在哪里,他们应该(即
Question.joins(:topics).where(:conditions => {:topics => {:id => [list of topics,...]}})
失败,出现错误no such column: "Topics"."id"
条款不起作用。
任何帮助吗?谢谢!
UPDATE
我已经安装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解决方案...... –