N
N
Neodelf2015-03-24 20:31:34
Ruby on Rails
Neodelf, 2015-03-24 20:31:34

How can I use the Tire gem to search by the fields of a linked table?

Hello.
There is a model with the following code:

mapping do
  indexes :id, type: 'integer'
  indexes :name, type: 'string'
  indexes :address, type: 'string'
  indexes :offices_pub_count, type: 'integer'

  indexes :offices do
    indexes :retail, type: 'boolean'
  end
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}

But here, a search with filtering not for the field of offices gives an empty array:
BusinessCenter.search(:load => { :include => 'offices' }) do
  query { string '*' }
  filter :term, "offices.retail" => true
end

Who solved such a problem?
PS Actually, the example on the link https://gist.github.com/karmi/3200212 doesn't work

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
Boris Penkovsky, 2015-03-24
@Able1991

And this is not forgotten, as in the example?

class Chapter < ActiveRecord::Base
  # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  # Do not forget to automatically `touch` parent object from associations
  belongs_to :book, touch: true
  # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
end

and in the model for which we are looking for
When adding a related object, the index by record should be updated

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question