Querying in rails returns empty when testing

I’m using the webmock template you’ve provided. When I’m looking at the source for that template I see that it returns objectID 42 as a match for every query.

However - when I run Model.search the results returned are always empty.

I’m probably missing something as usual when issues like this arise, but could you point me in the right direction?

The specs are fairly simple:

 it "should find a user" do
    user = create(:user, id: 42)
    fill_in "search", with: user.email
    expect(page).to have_content(user.first_name)
  end

WIth user model containing this:

algoliasearch do
    attribute :first_name, :last_name, :email, :phone
    searchableAttributes ['first_name', 'last_name', 'email', 'phone']
end

and the search controller just doing this simple search:

@users = User.search(search_string)

I’ve verified with byebug that the search_string is intact and that User.search indeed just returns an empty array.

Hi Joakim,

thank you for reaching out about that. That’s indeed weird :confused:

By default, the MyModel.search method will try to load the matching objectIDs from your DB in order to let you manipulate ActiveRecords -> could you make sure the user you save in your test is well created with id 42?

Could you try with raw_search instead of search to bypass the ActiveRecord layer and get back JSON?

Let us know how that goes!

Hey Sylvain, thanks for the quick reply.

I’ve verified that the records indeed are saved and available via a standard User.find(42). new_record? returns false.

Here’s running raw_search and search for comparison.

User.search(search_string)
-> []
User.raw_search(search_string)
-> {"hits"=>[{"objectID"=>42}], "page"=>1, "hitsPerPage"=>1}

So weird :confused: Could you try to debug adding some logs in https://github.com/algolia/algoliasearch-rails/blob/master/lib/algoliasearch-rails.rb#L657-L667 -> there is something we don’t get here :slight_smile:

Yeah, sure! I’ll provide some logs tomorrow!

Ok, so I’ve found where it breaks!

[659, 668] in /Users/wimmerstedt/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/algoliasearch-rails-    1.20.0/lib/algoliasearch-rails.rb
   659:         algolia_object_id_of(hit)
   660:       end
   661:       results = json['hits'].map do |hit|
   662:         o = results_by_id[hit['objectID']]
   663:         byebug
=> 664:         if o
   665:           o.highlight_result = hit['_highlightResult']
   666:           o.snippet_result = hit['_snippetResult']
   667:           o
   668:         end
(byebug) algoliasearch_options[:type]
Spot(id: integer, number: string, storey: string, garage_id: integer, created_at: datetime, updated_at: datetime, classification_id: integer)
(byebug) results_by_id
{"42"=>#<Spot id: 42, number: "1", storey: "1", garage_id: 9178, created_at: "2017-08-03 13:24:08", updated_at: "2017-08-03 13:24:08", classification_id: 4606>}
(byebug) o
nil
(byebug) hit
{"objectID"=>42}
(byebug) hit['objectID']
42
(byebug) results_by_id[hit['objectID']]
nil
(byebug) results_by_id[hit['objectID'].to_s]
#<Spot id: 42, number: "1", storey: "1", garage_id: 9178, created_at: "2017-08-03 13:24:08", updated_at: "2017-08-03 13:24:08", classification_id: 4606>

So the issue is simple - the hash key is a string but the ID is an int. What I can’t understand is how this could work in development at all :smile:

Went ahead and created that 4 character PR here: https://github.com/algolia/algoliasearch-rails/pull/251