exceptionite icon indicating copy to clipboard operation
exceptionite copied to clipboard

"Model method does not exist" solution can be improved

Open os-nikita opened this issue 2 years ago • 6 comments

If the model is brand new, hasn't been created yet (not model.is_created() == True), then trying to access a property on the model like if not user.picture: ... will result in this error. The solution offered can be confusing.

os-nikita avatar May 18 '22 12:05 os-nikita

I am not sure to see what you mean. Could you post a code snippet so that I can reproduce the issue ? 🙏

girardinsamuel avatar May 19 '22 16:05 girardinsamuel

I think the way to replicate it is this:

Screen Shot 2022-06-05 at 5 30 37 PM

Results in this error Screen Shot 2022-06-05 at 5 30 43 PM

josephmancuso avatar Jun 05 '22 21:06 josephmancuso

The error seems clear to me and i'm not sure what you find confusing about it. I'll reopen and fix if you can provide a solution that makes sense to you

josephmancuso avatar Jun 05 '22 21:06 josephmancuso

@josephmancuso

Here's what I mean. This throws an exception, rather than rendering the home view:

image

It's not about a calling a method on a model. It's about the fact that the model hasn't been persisted yet and attempting to access an attribute/property (not a method), will throw this error.

If I change the above slightly to the following, the error isn't thrown. The email is returned:

image

os-nikita avatar Jun 06 '22 07:06 os-nikita

Ok so Ill add a possible solution like

Title: Model has not been created or retrieved

Solution: Make sure the model is correctly created or retrieved from the database before accessing attributes.

josephmancuso avatar Jun 06 '22 20:06 josephmancuso

How you are going to differentiate a normal class from a model class ? Because it should be a solution for Masonite Model classes only... 🤔

girardinsamuel avatar Oct 09 '22 17:10 girardinsamuel