Fix nil FK queries, strict_where validation, field name conflict warnings#348
Closed
sawirricardo wants to merge 1 commit into
Closed
Fix nil FK queries, strict_where validation, field name conflict warnings#348sawirricardo wants to merge 1 commit into
sawirricardo wants to merge 1 commit into
Conversation
…onflicts Fixes active-hash#302: belongs_to/has_one no longer issue queries when foreign key is nil. Early return nil instead of calling find_by_* with nil, matching AR behavior. Fixes active-hash#190: Add opt-in strict_where class attribute. When enabled, where() raises ArgumentError for unknown field names, catching typos like where(typo_key: nil) that silently returned all records. Fixes active-hash#184: Warn when field name conflicts with an existing method (e.g. :display added to Object by Rails). Previously silently returned nil.
Collaborator
|
@sawirricardo Thanks very much for opening this PR! However, if this is three individual fixes, we would prefer it to be broken out into three separate pull requests, so each change can be reviewed and merged on its own. Also, I would like to see tests added that clearly describe the problem being fixed. If you need any help figuring out to craft an appropriate test, I'm happy to help -- just drop a note here! I appreciate your time -- thanks for understanding how we like to work. I hope you're able to make time to contribute in the future! ❤️ |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Three low-hanging fixes addressing open issues.
Fixes #302 — belongs_to with nil foreign key makes useless query
When accessing a
belongs_toassociation with a nil foreign key, ActiveHash was still callingfind_by_*which triggers a DB query. ActiveRecord skips this query when the key is nil.Fix: Early return
nilwhen foreign key is nil in:ActiveRecordExtensions#belongs_tobelongs_to_active_hashMethods#belongs_toMethods#has_oneNo breaking change — nil key previously returned nil anyway after a useless query.
Fixes #190 —
where(undefined_key: nil)returns all recordswhere()with an undefined field silently returns all records becauseread_attributereturns nil for unknown keys, and nil == nil matches everything.Fix: Opt-in
strict_whereclass attribute. When enabled:Also validates against data keys for models that use undeclared fields via
data=. Opt-in only — no breaking change.Fixes #184 —
field :displaysilently returns nilRails adds
displayto Object. ActiveHash skips defining the method if one already exists, but gives no indication. Users get silent nil returns and confusing test failures.Fix:
warn()invalidate_fieldwhen field name conflicts with an existing method:Additive warning only — no breaking change.
Test Results
276 specs pass, 2 pre-existing sqlite3 adapter failures unrelated to these changes.