思考狮身人面像搜索has_many没有工作没有错误

时间:2014-01-24 15:13:33

标签: ruby-on-rails-3 has-many thinking-sphinx

型号

class Person < ActiveRecord::Base
  attr_accessible :alignment, :description, :first_name, :last_name
  has_many :roles    #table roles with active as one of the field with value equal t or f (boolean)
end    

class Role < ActiveRecord::Base
  attr_accessible :active, :organization_id, :person_id, :position_id
  belongs_to :person 
  belongs_to :organization 
  belongs_to :position
end   

person_index.rb

ThinkingSphinx::Index.define :person, :with => :active_record do
 #Fields
 indexes last_name, :sortable => true
 indexes first_name, :sortable => true
 indexes alignment
 #indexes role(:active), :as => :active
 indexes role.active, :as => :active


 #Attributes
  has created_at, updated_at
  has professions(:id), :as => :profession_ids
  has positions(:id), :as => :position_id
  has organizations(:id), :as => :organization_id

end

people_controller

 filters = {}
 filters[:profession_ids] = params[:profession_ids] if params[:profession_ids].present?
 filters[:organization_id] = params[:organization_id] if params[:organization_id].present?
 filters[:position_id] = params[:position_id] if params[:position_id].present?   
 filters[:active_ids] = role if params[:active].present?  #added   

 @people = Person.search " #{params[:lastname]} #{params[:firstname]} #{params[:alignmemt]}", 
 :with     => filters,
 :star => true, 
 :condition =>  {:alignment => params[:alignment], :active =>  params[:active]}, 
 :order => 'last_name ASC, first_name ASC',
 :page     => params[:page], 
 :per_page => 20    

当我搜索active和/或alignment时,它不会过滤结果并且不会给我错误。这些都是字符串字段,alignment在人员表中,active在另一个表中(角色)

为什么呢?我错过了什么?

更新
this question上尝试了active的推荐解决方案,结果相同......

person_index.rb

ThinkingSphinx::Index.define :person, :with => :active_record do
 #Fields
 indexes last_name, :sortable => true
 indexes first_name, :sortable => true
 indexes alignment

 #Attributes
  has created_at, updated_at
  has professions(:id), :as => :profession_ids
  has positions(:id), :as => :position_id
  has organizations(:id), :as => :organization_id
  has roles.active, :as => :active_ids

end

people_controller

def index
 @role = Role.find_by_active(params[:active])   #ADDED

 filters = {}
 filters[:profession_ids] = params[:profession_ids] if params[:profession_ids].present?
 filters[:organization_id] = params[:organization_id] if params[:organization_id].present?
 filters[:position_id] = params[:position_id] if params[:position_id].present?

 @people = Person.search " #{params[:lastname]} #{params[:firstname]} #{params[:alignmemt]}", 
 :with     => filters,
 :star => true,
 :condition =>  {:alignment => params[:alignment], :active_ids =>  @role}, #CHANGED
 :order => 'last_name ASC, first_name ASC',
 :page     => params[:page], 
 :per_page => 20     

但仍然有相同的结果......为什么?

Pat回答后

控制器更新

def index   
  if params[:active].present?
    role = Array.new
    rolepid = Array.new
    role = Role.find_all_by_active(params[:active])
    role.each do |num|
     puts num.person_id
     rolepid << num.person_id  #get all the person id whith the params[:active]
    end
  end

  filters = {}
  filters[:profession_ids] = params[:profession_ids] if params[:profession_ids].present?
  filters[:organization_id] = params[:organization_id] if params[:organization_id].present?
  filters[:position_id] = params[:position_id] if params[:position_id].present?
  filters[:active_ids] = rolepid if params[:active].present?

  @people = Person.search " #{params[:lastname]} #{params[:firstname]} #{params[:alignent]}", 
  #:classes => [Person, Role],
  :with     => filters,
  :star => true, 
  :condition =>  {:alignment => params[:alignment]},
  :order => 'last_name ASC, first_name ASC',
  :page     => params[:page], 
  :per_page => 20

但现在它正在寻找人员表中的活动,它应该在角色表中查找。所以我添加#:classes => [Person, Role],但没有运气......

 Role Load (0.7ms)  SELECT "roles".* FROM "roles" WHERE "roles"."active" = 'f'
 Sphinx Query (0.7ms)  SELECT * FROM `person_core` WHERE `active_ids` IN (304, 34, 306, 308, 334, 295, 344, 348, 352, 354, 365, 367, 308, 429, 468, 9, 544, 590, 609, 110, 1643, 1652, 1653, 1655, 1669, 628, 1687, 1691, 1709) AND `sphinx_deleted` = 0 ORDER BY `last_name` ASC, first_name ASC LIMIT 0, 20
 Sphinx  Found 0 results

所以我在控制器中更改 filters[:active_ids] = rolepid if params[:active].present?

 filters[:id] = rolepid if params[:active].present?

由于rolepid是一个带有人ID的整数数组 但是Sphinx正在寻找不在rolepid中的4个ID ...我很困惑:|

 Parameters: {"utf8"=>"✓", "firstname"=>"", "lastname"=>"", "alignment"=>"", "organization_id"=>"", "position_id"=>"", "active"=>"f", "commit"=>"Search"}
 Role Load (0.8ms)  SELECT "roles".* FROM "roles" WHERE "roles"."active" = 'f'
 Sphinx Query (0.6ms)  SELECT * FROM `person_core` WHERE `id` IN (304, 34, 306, 308, 334, 295, 344, 348, 352, 354, 365, 367, 308, 429, 468, 9, 544, 590, 609, 110, 1643, 1652, 1653, 1655, 1669, 628, 1687, 1691, 1709) AND `sphinx_deleted` = 0 ORDER BY `last_name` ASC, first_name ASC LIMIT 0, 20
 Sphinx  Found 4 results
 Person Load (0.4ms)  SELECT "people".* FROM "people" WHERE "people"."id" IN (84, 1, 61, 50)   

为什么不从rolepid数组返回29条记录?

过滤alignment IS正在运行。谢谢你抓住拼写错误的单词。

1 个答案:

答案 0 :(得分:1)

如果您使用active_ids作为属性(如果它是整数,那当然是合适的),那么它应该是:with选项中的过滤器,而不是:conditions中的过滤器选项。

我不确定这是否相关,但值得注意的是你在查询字符串中拼错了对齐(你已经改为使用了alignmemt)。