Description
I had AI write some code for me and it wrote out a displayed_attributes line but didn't include the id, this caused no results to always be returned. I used AI to expand on what is going on, please forgive me. Basically, an error should be shown or it should always include the primary id.
Since internally the search results to fetch records from the database via .where(id: hit_ids), the absence of the ID in the Meilisearch response prevents the gem from mapping hits back to ActiveRecord/Sequel models. The library should either automatically include the primary key in displayed_attributes or raise a configuration error when it is missing.
Expected behavior
The gem should successfully return model instances even if :id is not explicitly listed in displayed_attributes, OR it should warn the developer that the primary key is required for the "Search -> DB Lookup" flow to function.
Current behavior
The search returns a Meilisearch::Rails::Pagination object where the results are empty (or contain nil values) because hit_ids evaluates to [nil].
Environment
Operating System: Ubuntu / Linux Mint
Meilisearch version: v1.32.2
meilisearch-rails version: v0.16.0
rails version: v8.0.2
Reproduction script:
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
ruby '3.3.0' # Adjust to your local version
gem 'minitest'
gem 'rails', '~> 8.0'
gem 'sqlite3'
gem 'meilisearch-rails'
end
require 'minitest/autorun'
require 'active_record'
# Setup Meilisearch
Meilisearch::Rails.configuration = {
meilisearch_host: ENV.fetch('MEILISEARCH_HOST', 'http://127.0.0.1:7700'),
meilisearch_api_key: ENV.fetch('MEILISEARCH_API_KEY', 'masterKey'),
}
# ActiveRecord Setup
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
ActiveRecord::Schema.define do
create_table :books do |t|
t.string :title
t.string :author
end
end
class Book < ActiveRecord::Base
include Meilisearch::Rails
meilisearch do
attribute :title, :author
searchable_attributes [:title]
# BUG: Omitting :id here causes ms_search to fail
displayed_attributes [:title, :author]
end
end
class BugTest < Minitest::Test
def setup
Book.destroy_all
Book.create!(title: "The Great Gatsby", author: "F. Scott Fitzgerald")
Book.reindex!
# Wait for Meilisearch to process the index
task = Meilisearch::Rails.client.tasks['results'].first
Meilisearch::Rails.client.wait_for_task(task['uid'])
end
def test_search_fails_when_id_is_not_displayed
results = Book.search("Gatsby")
# This will fail. results.count will be 0 or results will contain [nil]
# because the gem couldn't find the 'id' in the hits JSON to perform the DB lookup.
assert_equal 1, results.count, "Search should return 1 book even if id is not in displayed_attributes"
assert_instance_of Book, results.first
end
end
Description
I had AI write some code for me and it wrote out a displayed_attributes line but didn't include the id, this caused no results to always be returned. I used AI to expand on what is going on, please forgive me. Basically, an error should be shown or it should always include the primary id.
Since internally the search results to fetch records from the database via
.where(id: hit_ids), the absence of the ID in the Meilisearch response prevents the gem from mapping hits back to ActiveRecord/Sequel models. The library should either automatically include the primary key indisplayed_attributesor raise a configuration error when it is missing.Expected behavior
The gem should successfully return model instances even if
:idis not explicitly listed indisplayed_attributes, OR it should warn the developer that the primary key is required for the "Search -> DB Lookup" flow to function.Current behavior
The search returns a
Meilisearch::Rails::Paginationobject where the results are empty (or containnilvalues) becausehit_idsevaluates to[nil].Environment
Operating System: Ubuntu / Linux Mint
Meilisearch version: v1.32.2
meilisearch-rails version: v0.16.0
rails version: v8.0.2
Reproduction script: