2014-01-24 81 views
0

模式思維獅身人面像搜索的has_many不工作沒有錯誤

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在另一個表格中(角色)

爲什麼?我錯過了什麼?

更新
受審activethis question和相同的結果所推薦的解決方案......

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  

但仍然有相同的結果...爲什麼?

控制器帕特更新後回答

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只是尋找4個ID不在rolepid ...我很困惑:|

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正在工作。感謝您收到拼寫錯誤的單詞。

回答

1

如果您使用active_ids作爲屬性(如果它是整數,肯定是合適的),那麼它應該是:with選項中的篩選器,而不是:conditions選項中的篩選器。

我不確定這是否相關,但值得注意的是,您在查詢字符串中拼錯對齊(您已改爲alignmemt)。

+0

感謝您的答案,但我仍然卡住...... 1)我用:與積極屬性這是一個整數數組,但沒有運氣。 2)關於對齊糾正了拼寫錯誤的世界,但沒有任何或者...... – mamesaye

+0

2)'alignment'拼寫錯了兩次。它正在工作。謝謝 – mamesaye

+0

得到的答案我不得不使用sphinx_internal_id,所以我刪除'過濾器[:id] =的角度如果參數[:活動] .present?',並用'過濾器[:sphinx_internal_id] = rolepid'替換它。有關更多解釋,請轉到此處:https://groups.google.com/forum/?pli=1#!topic/thinking-sphinx/NwsMsN7ozpU – mamesaye

相關問題