-
Notifications
You must be signed in to change notification settings - Fork 64
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
Raise error if required association not loaded in preload #812
Raise error if required association not loaded in preload #812
Conversation
@@ -30,6 +30,12 @@ module Avram | |||
end | |||
end | |||
|
|||
class MissingRequiredAssociationError < AvramError | |||
def initialize(model : Avram::Model.class, association : Avram::Model.class) | |||
super "Expected #{model} to have an association with #{association} but one was not found." |
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.
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.
Maybe but none were found.
?
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.
How about Expected Comment to be assigned to a Post but no association was supplied
?
Or Expected Comment to belong to a Post but no Post was supplied
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.
Whoops, forgot to wait for feedback on this 😬 Feel free to open a PR with an error message that you think is better. Otherwise I might remember to make one at some point
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.
Well. I could probably have also looked to see if it was merged before trying to be helpful...
OMG 🤣 I should have looked at this before writing my previous comment on the issue... |
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.
Ok, this wasn't exactly what I was thinking, but I think this is still pretty good 👍
Related #672
If a model defines a required belongs to association and the record isn't retrieved in a preload, this raises an appropriate error. Also added for required has one associations.
Applying this to the related issue, in the scenario where you have soft deletes, the solution now becomes making what was a required association optional since it might not always be loaded. This doesn't change whether or not it is loaded but will guarantee an error unless you change the association. Before, the error only occurred if you tried to access the association on the particular record.