Search code examples
ruby-on-railsruby-on-rails-4elasticsearchtire

search has_many tables contents with elasticsearch rails 4


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

I am new in "elasticsearch".I am able to search "reports" through the above code.But how do i include "schedules" so that i can search its data in Entity model.Please help me out.


Solution

  • Add the following line to your Entity model after has_many :reports,:dependent=>:destroy

    has_many :schedules, :through => :reports
    

    And make sure you have belongs_to :report in your Schedule model.