feat: Add attribute_always_select? option to belongs_to relationships#2627
Open
emadshaaban92 wants to merge 1 commit intoash-project:mainfrom
Open
feat: Add attribute_always_select? option to belongs_to relationships#2627emadshaaban92 wants to merge 1 commit intoash-project:mainfrom
emadshaaban92 wants to merge 1 commit intoash-project:mainfrom
Conversation
Contributor
|
There is an option called |
zachdaniel
approved these changes
Mar 12, 2026
Contributor
|
Please make sure to run |
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.
Contributor checklist
Leave anything that you believe does not apply unchecked.
Problem
When reading records with a limited select, foreign key attributes from belongs_to relationships get masked as
Ash.NotLoaded. If you then call an update action on that record, any change with awhere [present(:foreign_key)]guard will silently skip — even though the value exists in the database.Example
Consider a Task resource that optionally belongs to a Project:
When a task is completed, we want to check if the entire project is now done and update its status — but only for project-linked tasks:
In a dashboard LiveView, tasks are loaded with a limited select for performance:
When a user marks a task as complete, the action runs on the record as-is. Since
project_idwasn't in the select, it'sAsh.NotLoaded,present?returnsfalse, and theAutoCompleteProjectchange is silently skipped — even for tasks that do belong to a project.The current workaround is to either:
always_select? trueand setdefine_attribute? false(verbose)Solution
Adds attribute_always_select? to belongs_to, consistent with existing attribute_writable? and attribute_public? options:
Alternative / Future Consideration
ensure_loaded or preload on actions
A complementary approach would be an
ensure_loadedoption that loads specific fields on the record before running validations and changes. Unlikeattribute_always_select?(which is attribute-scoped and global),ensure_loadedwould generalize to calculations, aggregates, and relationships, and could be scoped to specific actions.Note: Code in this PR is hand-written. The PR description was generated with AI assistance.