0

我正在开发的应用程序有 3 个主要模型和许多单表继承模型:

  1. 问题
  2. 用户
    1. 专业的
    2. 代表
  3. 分类
    1. 类别
    2. 话题
    3. 职业
    4. 地方性
    5. 地区
    6. 国家

有多种用户(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"

现在,在设置完所有这些之后,事情就不太顺利了......

  1. 我似乎无法将分类法分配给专业人士或问题(即Question.create(:title => "Lorem Ipsum Dolor Sit Amet", :author => current_user, :topics => [list of topics,...])除了未保存的主题外效果很好。)

  2. 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'

知道如何为两者修复它吗?

4

1 回答 1

3

对于问题 #2,where 子句中的键应该映射到表名,而不是关联名。所以我想你会想要:

Question.joins(:topics).where(Topic.table_name => {:id => [...]})

对于问题 #1,似乎当您设置时question.topics = [...],Rails 创建的分类对象正在设置为“分类法”(而不是“主题”)的 taxonomy_type。这似乎是由于 Rails 的 through_association.rb:51,它采用了base_class正在存储的模型,而不仅仅是实际的类名。

我能够通过分类模型上的 before_validation 回调来解决这个问题。在我看来,替代方法是对实际的 Rails 关联代码进行补丁,以使这种行为可配置。

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
  before_validation :set_valid_types_on_polymorphic_associations

  protected

  def set_valid_types_on_polymorphic_associations
    self.classifiable_type = classifiable.class.model_name if classifiable
    self.taxonomy_type = taxonomy.class.model_name if taxonomy
  end
end

更新

似乎有另一个 Rails 决定(在preloader/association.rb:113中)在设置关联范围时使用model.base_class.sti_name而不是。model.sti_name

那颗宝石应该为你处理好这件事。请参阅store_base_sti_class_for_3_1_and_above.rb:135了解它如何包装has_many :as选项。在我的本地环境中,这按预期工作:

$ bundle exec rails console
irb(main):001:0> topics = 3.times.map { Topic.create }
irb(main):002:0> p = Professional.new
irb(main):003:0> p.topics = topics
irb(main):004:0> p.save!
irb(main):005:0> exit

$ bundle exec rails console
irb(main):001:0> puts Professional.find(1).topics.to_sql
SELECT "taxonomies".* FROM "taxonomies" INNER JOIN "classifications" ON "taxonomies"."id" = "classifications"."taxonomy_id" WHERE "taxonomies"."type" IN ('Topic') AND "classifications"."classifiable_id" = 2 AND "classifications"."classifiable_type" = 'Professional' AND "classifications"."taxonomy_type" IN ('Topic')
irb(main):002:0> Professional.find(1).topics.count
=> 3
于 2012-12-31T17:42:34.487 回答