Skip to content
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

Merged

Conversation

matthewmcgarvey
Copy link
Member

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.

@@ -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."
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Error message could be better. Any ideas?

Current:
Screen Shot 2022-02-14 at 11 33 36 PM

Copy link
Member

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.?

Copy link
Contributor

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

Copy link
Member Author

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

Copy link
Contributor

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...

@jwoertink
Copy link
Member

OMG 🤣 I should have looked at this before writing my previous comment on the issue...

Copy link
Member

@jwoertink jwoertink left a 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 👍

@matthewmcgarvey matthewmcgarvey merged commit 9251a96 into luckyframework:main Feb 15, 2022
@matthewmcgarvey matthewmcgarvey deleted the missing-assoc-error branch February 15, 2022 17:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants