model
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
When i search active
and/or alignment
it is not filtering result and doesn't give me error. these are both string field, alignment
is in the people table and active
is in another table (roles)
Why? What am i missing?
update
Tried the recommended solution for active
on this question and same result...
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
but still have same result... WHY?
controller updated after Pat answer
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
But now it is looking for active in people table when it should look in roles table. So i added #:classes => [Person, Role],
but no luck....
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
So i change in the controller
filters[:active_ids] = rolepid if params[:active].present?
to
filters[:id] = rolepid if params[:active].present?
Since rolepid
is an array of integer with the person ids.
But Sphinx
is just looking for 4 ids that are not in rolepid
... I am confused :|
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)
Why is it not returning the 29 records from rolepid
array ?
filtering for alignment
IS working. thanks for catching the misspelled word.
If you're using active_ids
as an attribute (which, if it's integers, is certainly appropriate), then it should be a filter in the :with
option, not in the :conditions
option.
I'm not sure if this is related, but it's worth noting you've misspelled alignment in the query string (you've got alignmemt instead).