Search code examples
ruby-on-railsruby-on-rails-3sunspot

How can I enable nested search sunspot solr?


I have two models: User and User_profile

In User.rb, User has_one :user_profile.

In User_profile.rb, User_profile belongs_to :user.

Then I have users_controller and its index.html.erb. So I added this to my User model in order to enable search by nested column, but it shows an error when I try to reindex. How can I fix this?

undefined method `user_profiles' for #

models/user.rb

....
  searchable do 
    text :user_profile_nickname do
      user_profiles.map { |user_profile| user_profile.nickname }
    end
  end
....

Solution

  • You have given User has_one :user_profile in your user.rb file. So it can't be collection of objects, rather it is a member object. So you can not call user_profiles.map { |user_profile| user_profile.nickname } in your User model.

    If you want to call write as following:

    searchable do 
      text :user_profile_nickname do
        user_profile.nickname
      end
    end
    

    The above will work for sure. Do let me know if not working.