Rails Tire Search In Associations has_many

124 views Asked by At

This is my model:

models/business_center.rb:

mapping do
  indexes :id, type: 'integer'
  indexes :name, type: 'string'
  indexes :address, type: 'string'
  indexes :offices_pub_count, type: 'integer'
  indexes :subtype_id, type: 'integer'
  indexes :offices_pub_retail_count, type: 'integer'
  indexes :metro_id, type: 'integer'
  indexes :area_id, type: 'integer'
  indexes :district_id, type: 'integer'
  indexes :latitude, type: 'integer'
  indexes :longitude, type: 'integer'

  indexes :offices do
    indexes :id,      type: 'integer'
    indexes :desc,      type: 'string'
    indexes :floor,      type: 'string'
    indexes :inner_info,      type: 'string'
    indexes :decoration,      type: 'string'
    indexes :fire_safety,      type: 'string'
    indexes :air_conditioning,      type: 'string'
    indexes :parking,      type: 'string'
    indexes :planning,      type: 'string'
    indexes :commercial_terms,      type: 'string'
    indexes :operation_cost_id,      type: 'string'
    indexes :retail,      type: 'boolean'

end

def as_indexed_json(options={})
  to_json(methods: [:offices_pub_count],
      include: {offices: { only: [:text,:desc,:floor,:inner_info,:decoration,:fire_safety,:air_conditioning,:parking,:planning,
                                     :commercial_terms,:operation_cost_id, :retail]
                }
      }

def offices_pub_count
  offices_pub.size
end

has_many :offices, :dependent => :destroy
has_many :offices_pub, class_name: 'Office', foreign_key:   'business_center_id', conditions: {published: true}    

And next time I try search as:

BusinessCenter.search(:load => { :include => 'offices' }) do
    query { string '*' }
    filter :term, "offices.retail" => true
end

and its get empty array

 => #<Tire::Results::Collection:0x0000000641fa90 @response={"took"=>4, "timed_out"=>false, "_shards"=>{"total"=>5, "successful"=>5, "failed"=>0}, "hits"=>{"total"=>0, "max_score"=>nil, "hits"=>[]}}, @options={:load=>{:include=>"offices"}, :size=>10}, @time=4, @total=0, @facets=nil, @max_score=0.0, @wrapper=Tire::Results::Item>

Where I make mistake? How filtered request with attribute in has_many model (in my case its the Office)?

I saw many examples, but they are work with has_one or belongs_to associations. Example in https://gist.github.com/karmi/3200212 now work. Please, help me!)

1

There are 1 answers

0
Neodelf On

As well I know, Tire don`t support this feature. Chewy (develops by TopTal) gem may do simple search in association model well. You should use him or something else, but Tire its wrong way. Its true)