使用elasticsearch rails 4搜索has_many表内容

时间:2015-11-30 20:17:49

标签: ruby-on-rails ruby-on-rails-4 elasticsearch tire

class Entity < ActiveRecord::Base
 include Tire::Model::Search
 include Tire::Model::Callbacks
 has_many :reports,:dependent=>:destroy

 def self.search(params)
    tire.search() do
     query { string params[:query] } if params[:query].present?         
    end
 end

 def to_indexed_json
   to_json(:include => [:reports])
 end

end

class Report < ActiveRecord::Base 
 belongs_to :entity
 has_many :schedules,:dependent=>:destroy
end

我是“elasticsearch”的新手。我可以通过上面的代码搜索“报告”。但是我如何包含“时间表”以便我可以在实体模型中搜索其数据。请帮助我。

1 个答案:

答案 0 :(得分:1)

Entity

之后,将以下行添加到has_many :reports,:dependent=>:destroy模型中
has_many :schedules, :through => :reports

确保belongs_to :report模型中有Schedule