Description
meilisearch-rails caches per-model configurations using a very generic class instance variable name: @configurations (on the model class). This name is common enough that it can easily collide with other libraries that extend the same model class.
I observed a concrete collision when algoliasearch-rails is also included in the same model: both gems use @configurations on the model class, and whichever runs last overwrites the cache. As a result, meilisearch-rails can read a foreign configuration (Algolia settings) and send it to Meilisearch’s settings API, causing 400 errors.
Even if the “Algolia + Meilisearch in one model” use case is not common, the underlying problem is broader: @configurations is a high-risk ivar name for a public integration gem, because the collision pattern can happen with any other gem that uses the same ivar name on the same model class.
Expected behavior
meilisearch-rails should not be affected by unrelated gems that extend the same model.
- Internal caches should be isolated (or namespaced) so that configuration/state cannot be overwritten externally.
Current behavior
When another gem uses @configurations on the same model class, meilisearch-rails may read the wrong cache and attempt to update Meilisearch settings with invalid keys, resulting in errors like:
400 Bad Request - Unknown field ranking
- warnings about keys such as
indexLanguages, queryLanguages, attributesForFaceting, customRanking
Screenshots or logs
Example (Rails console):
MyBook.ms_set_settings
# => may succeed
MyBook.last.index! # Algolia indexing path
MyBook.ms_set_settings
# WARN: Non-conforming attributes: searchableAttributes, attributesForFaceting, customRanking, queryLanguages, indexLanguages
# Meilisearch::ApiError: 400 Bad Request - Unknown field `ranking`
Also, MyBook.send(:ms_configurations).keys can include non-Meilisearch option hashes (e.g. :index_name, :replica, :primary_settings), indicating the Meilisearch configuration cache was overwritten.
Environment
Operating System: macOS (Rails in Docker)
Meilisearch version (./meilisearch --version): Meilisearch Cloud (Meilisearch version v1.42.1)
meilisearch-rails version (bundle info meilisearch-rails): 0.16.0
rails version (bundle info rails): 7.0.8.7
Reproduction script:
High-level reproduction (any Rails app where another gem sets @configurations on the same model class):
- Include
Meilisearch::Rails in a model and define a meilisearch block.
- Load/use another library that caches something on the same model class using
@configurations.
- Call
Model.ms_set_settings or record.ms_index!.
Observed reproduction with algoliasearch-rails:
MyBook.ms_set_settings
MyBook.last.index! # algoliasearch-rails
MyBook.ms_set_settings # may fail with Meilisearch::ApiError
Suggested fix
Avoid using a generic class ivar name like @configurations on the model class. Use a gem-specific name (e.g. @meilisearch_configurations) or isolate the cache in a dedicated structure to prevent collisions with other integrations that extend the same model.
Description
meilisearch-railscaches per-model configurations using a very generic class instance variable name:@configurations(on the model class). This name is common enough that it can easily collide with other libraries that extend the same model class.I observed a concrete collision when
algoliasearch-railsis also included in the same model: both gems use@configurationson the model class, and whichever runs last overwrites the cache. As a result,meilisearch-railscan read a foreign configuration (Algolia settings) and send it to Meilisearch’s settings API, causing 400 errors.Even if the “Algolia + Meilisearch in one model” use case is not common, the underlying problem is broader:
@configurationsis a high-risk ivar name for a public integration gem, because the collision pattern can happen with any other gem that uses the same ivar name on the same model class.Expected behavior
meilisearch-railsshould not be affected by unrelated gems that extend the same model.Current behavior
When another gem uses
@configurationson the same model class,meilisearch-railsmay read the wrong cache and attempt to update Meilisearch settings with invalid keys, resulting in errors like:400 Bad Request - Unknown field rankingindexLanguages,queryLanguages,attributesForFaceting,customRankingScreenshots or logs
Example (Rails console):
Also,
MyBook.send(:ms_configurations).keyscan include non-Meilisearch option hashes (e.g.:index_name,:replica,:primary_settings), indicating the Meilisearch configuration cache was overwritten.Environment
Operating System: macOS (Rails in Docker)
Meilisearch version (
./meilisearch --version): Meilisearch Cloud (Meilisearch version v1.42.1)meilisearch-rails version (
bundle info meilisearch-rails):0.16.0rails version (
bundle info rails):7.0.8.7Reproduction script:
High-level reproduction (any Rails app where another gem sets
@configurationson the same model class):Meilisearch::Railsin a model and define ameilisearchblock.@configurations.Model.ms_set_settingsorrecord.ms_index!.Observed reproduction with
algoliasearch-rails:Suggested fix
Avoid using a generic class ivar name like
@configurationson the model class. Use a gem-specific name (e.g.@meilisearch_configurations) or isolate the cache in a dedicated structure to prevent collisions with other integrations that extend the same model.