I'm starting to use searchkick in my app, and I have two models: House and Address (which belongs to a house).
House:
class House < ApplicationRecord
searchkick
has_one :address, dependent: :destroy
end
Address:
class Address < ApplicationRecord
searchkick
belongs_to :house
end
In my controller I have
def index
if params[:term].present?
@houses = House.search(params[:term])
else
@houses = House.search('*')
end
end
Right now, searchkick is working when I search for house data which is in the table houses
, but if for example I look for a field in the Addresses table, I do not get any result at all.
Is it possible to do this?
Thanks a lot in advance!
I think you need to index addresses also. To do that add the below code to your House
model:
def search_data
attributes.merge(
address_city: self.address.try(:city),
address_zip: self.address.try(:zip)
)
end
Feel free to modify the above code based on your fields in the Address
model.
Also, after adding the above code, don't forget to reindex.