Search code examples
ruby-on-railssolrsunspotassociated-object

Sunspot Profile, Match search associated model attribute results in


Want to easy search all associated model attributes Asked before still problems with this:

Profile model

has_one :match

searchable do
  integer       :id
  string        :country
  string        :state
  string        :city
end

Match model

belongs_to :profile

searchable do
  integer :id
  string :looking_for_education do
   match.looking_for_education
  end      
  integer :age_from
  integer :age_to
end

ProfilesController#Index

def index

  @search = Sunspot.search Profile do

    with(:country, params[:country]) # this is a profile attribute
    with(:state,   params[:state])   # this is a profile attribute   
    with(:looking_for_education, "high school") # this should search *inside* 
                                                #the match attribute's, 
                                                #where **match** belongs_to 
                                                #**profile**
  end

  @profiles = @search.results

end

Edit #1

Rewrote the searchable block like in first answer suggestion with a :looking_for_education do block. Still fails with a undefined method `looking_for' for #

Added integer :id to the indexes still same issue :(


Solution

  • solution:

    I finally found the problem, I had some issues in my development database where Profile was not having a Match. + some missing profile_id in the match table, after fixing those the reindex went fine.