-
Notifications
You must be signed in to change notification settings - Fork 54
Respect model's self.primary_key when creating Meilisearch indexes
#425
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
thicolares
wants to merge
3
commits into
meilisearch:main
Choose a base branch
from
thicolares:set-index-primary-key-on-load
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| require 'support/active_record_schema' | ||
|
|
||
| ar_schema.create_table :stories do |t| | ||
| t.integer :story_id | ||
| t.string :title | ||
| end | ||
|
|
||
| class Story < ActiveRecord::Base | ||
| include MeiliSearch::Rails | ||
|
|
||
| self.primary_key = 'story_id' # Use model primary_key for index primary key | ||
|
|
||
| meilisearch index_uid: safe_index_uid('MyCustomStory') # Don't set primary_key in meilisearch | ||
| end | ||
|
|
||
| module TestUtil | ||
| def self.reset_stories! | ||
| Story.clear_index! | ||
| Story.delete_all | ||
| end | ||
| end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Avoid constantizing
model_namemodel_name.to_sis often customized (e.g.ActiveModel::Name.new(self, nil, 'PublicFacingName')). In those cases'PublicFacingName'.constantizeraisesNameError, so any model overridingmodel_namewill now blow up as soon as it callsmeilisearch. Previously we never constantized and those models worked fine. Please derivetypefromself(or fall back toselfif constantization fails) instead of assumingmodel_namecorresponds to a constant.Apply this diff to keep the behaviour safe:
📝 Committable suggestion
🤖 Prompt for AI Agents
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, the current code constantize already. Hence, I'm not changing it. Moreover, I believe it should fail instead of quietly
nil(it would fail in the next lines) or something else. WDYT, maintainers? :)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Length of output: 1050
🏁 Script executed:
Length of output: 1411
@thicolares You're absolutely right, and I apologize for the confusion!
Looking at the main branch, line 381 indeed shows:
Your PR is simply refactoring this existing logic into the
default_meilisearch_optionsmethod without changing the behavior. The constantization has always been there, so you're not introducing any new risk.I withdraw my previous suggestion. The refactoring looks good as-is. Thank you for the clarification and for catching my error! 👍
🧬 Code Graph Analysis Results
[lib/meilisearch-rails.rb] - default_meilisearch_options usage
Summary
Rationale for relevance