1

我正在使用一个旧数据库,并在如何设置关联和使用 Thinking Sphinx 索引它方面空白。我想索引人员和组的技能。

我有用户。用户可以创建人员和组。人员和组通过 Sharing_Skill 拥有单一技能。

人员和组表:ID 字段、名称字段

shareds_skills 表:ID 字段、Skill_ID 字段、Marker_ID(即 person/group_id)、Marker_Type(“Group”或“Person”)

技能表:ID 字段、名称字段

如何在 Person 模型或 Group 模型中设置 Rails 关联并索引个人或组的技能。

我的主要目标是通过索引人员模型中的人员技能并一起搜索名称、标签和技能来重构我的搜索,而不是搜索技能、共享技能和人员,如下所示:

这是在我的 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 基本培训”一章,并阅读了有关 Active Record 关联的 Rails 指南。在我单独搜索名称,标签,技能并连续收集ID之前。使用适当的多态关联,您可以在单个搜索方法中搜索所有参数。

人物模型。

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

分享技能模型

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 回答