简体   繁体   English

协会中的Rails轮胎搜索has_many

[英]Rails Tire Search In Associations has_many

This is my model: 这是我的模型:

models/business_center.rb: 车型/ 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)? has_many模型(在我的情况下是Office)中如何过滤具有属性的请求?

I saw many examples, but they are work with has_one or belongs_to associations. 我看到了很多示例,但是它们与has_one或belongs_to关联一起使用。 Example in https://gist.github.com/karmi/3200212 now work. https://gist.github.com/karmi/3200212中的示例现在可以使用。 Please, help me!) 请帮我!)

As well I know, Tire don`t support this feature. 众所周知,Tire不支持此功能。 Chewy (develops by TopTal) gem may do simple search in association model well. Chewy(由TopTal开发)gem可以很好地在关联模型中进行简单搜索。 You should use him or something else, but Tire its wrong way. 您应该使用他或其他东西,但是要忍受错误的方式。 Its true) 这是真的)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM