1

私は古いデータベースで作業しており、関連付けを設定して Thinking Sphinx でインデックスを作成する方法について空白を描いています。People と Group のスキルをインデックス化したい。

ユーザーがいます。ユーザーは、ユーザーとグループを作成できます。人およびグループは、Sharing_Skill を通じて単一のスキルを持ちます。

人物とグループのテーブル: ID フィールド、名前フィールド

sharings_skills テーブル: ID フィールド、Skill_ID フィールド、Marker_ID (個人/グループ ID)、Marker_Type (「グループ」または「個人」)

スキル テーブル: ID フィールド、名前フィールド

Rails の関連付けをセットアップし、Person モデルまたは Group モデルで個人またはグループのスキルをインデックス化するにはどうすればよいですか。

私の主な目標は、次のようにスキル、sharing_skills、および people を個別に検索するのではなく、person モデルで person スキルをインデックス化し、名前、タグ、およびスキルを一緒に検索することで、検索をリファクタリングすることです。

これは私の Person モデルにあります。

class Person < ActiveRecord::Base
  acts_as_taggable
  acts_as_taggable_on :tags

  attr_accessible :name, :tag_list

  validates_presence_of :name

  belongs_to :marker_image, :class_name => "Photo", :foreign_key => "marker_image_id"
  belongs_to :user

  extend FriendlyId
  friendly_id :name, use: [:slugged, :history]

  define_index do
    indexes name
    indexes tag_taggings.tag(:name), :as => :tags
    has :id
    has created_at
  end

これは私の検索コントローラーにあります

#PERSON SEARCH
if params[:marker_type].nil? or params[:marker_type].empty? or params[:marker_type] == "Person"
  #SEARCH NAME, TAGS, (Need to fix skills)
  conditions = {}
  %w(name tags).each do |i|
    i = i.to_sym
    next unless params[i]
    conditions[i] = params[i]
  end

  person = Person.search_for_ids(params[:search], :conditions => conditions, :per_page => 999999)
  if !person.empty?
    person_ids << person
  end

  #SEARCH SKILLS IN PERSON MODEL
  skills = Skill.search_for_ids(params[:search], :per_page => 99999)
  check_skills_available_persons = SharingsSkill.search(:conditions => {:marker_type => 'Person'}, :with => {:skill_id => skills}, :per_page => 99999)
  if !check_skills_available_persons.empty?
    check_people_by_skills = Person.search(:with => {:id => check_skills_available_persons.collect{|x|x.marker_id}})
    if !check_people_by_skills.empty?
      check_people_by_skills.each do |p_skill|
        person_ids << p_skill.id
      end
    end
  end
end
4

1 に答える 1

1

関連付けに関する「Rails 3 Essential Training」の章を見て、Active Record 関連付けに関する Rails ガイドの記事を読みました。以前は、名前、タグ、スキルを個別に検索し、ID を連続して収集していました。適切なポリモーフィック アソシエーションを使用すると、1 つの検索メソッドですべてのパラメーターを検索できます。

人物モデル。

has_one :sharing_skill, :as => :marker, :conditions => ['marker_type = ?', 'Person']
has_one :skill, :through => :sharing_skill

define_index do
  indexes name
  indexes tag_taggings.tag(:name), :as => :tags
  indexes skill(:name), :as => :skill
  has :id
  has created_at
end

グループモデル。

has_one :sharing_skill, :as => :marker, :conditions => ['marker_type = ?', 'Group']
has_one :skill, :through => :sharing_skill

define_index do
  indexes name
  indexes tag_taggings.tag(:name), :as => :tags
  indexes skill(:name), :as => :skill
  has :id
  has created_at
end

Sharing_skill.model

belongs_to :marker, :polymorphic => true
belongs_to :skill

スキルモデル。

has_one :sharing_skill
has_one :person, :through => :sharing_skill, :source => :marker
has_one :group, :through => :sharing_skill, :source => :marker

検索コントローラー。

#SEARCH NAME, TAGS, AND SKILL
conditions = {}
%w(name tags skill).each do |i|
  i = i.to_sym
  next unless params[i]
  conditions[i] = params[i]
end

person = Person.search(params[:search], :conditions => conditions, :per_page => 999999)
于 2012-11-06T00:38:49.127 に答える