私が開発しているアプリには、3 つの主要なモデルと多くの単一テーブル継承モデルがあります。
- 質問
- ユーザー
- プロ
- 代表者
- 分類法
- カテゴリー
- トピック
- 職業
- 地域性
- 領域
- 国
複数の種類のユーザー ( User
、Professional < User
、Representant < User
) があり、それらはすべて単一テーブル継承の User クラスから継承されます。
複数の種類のタクソノミ ( Category < Taxonomy
、Topic < Taxonomy
、Profession < Taxonomy
、Locality < Taxonomy
、 ) がありRegion < Taxonomy
、Country < 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"
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"
プロフェッショナル.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"
ます。
何か助けはありますか?ありがとう!
アップデート
示されているように、宝石「store_base_sti_class」をインストールしました。分類モデルに望ましい効果がありました。
#<Classification classifiable_id: 1, classifiable_type: "Professional", taxonomy_id: 17, taxonomy_type: "Topic">
ただし、トピック ( Professional.find(1).topics
) を照会すると、ActiveRecord は「Professional」ではなく「User」クラスを探しています...
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'
両方でそれを修正する方法はありますか?