When and how is the violation_message for a relationship used?

I want to show a localized error message when a postgres database constraint for a relationship is violated (especially, during the destroy action of a parent record that still has child records that belong to it).

The documentation for the violation_message suggests that it shall be used for that purpose. It is “A message to show if there is a conflict with this relationship in the database on destroy.”

However, if I specify a violation_message for the belongs_to relationship, upon trying to destroy the record I only get an InvalidAttribute error on field :id with the standard message “would leave records behind”. Having an inverse has_many relationship with a violation_message does not change this, either.

So how can I specify a relationship-specific, localized violation_message (using gettext) that is then used in the error (or elsewhere)? Or are error messages not intended to be localized at all? How to specify and use relationship-specific, localized error messages for an application, then?

Localization happens at the “edges”. So you would do it in your live views, or in an error handler for your GraphQL etc.

With that said, if the violation_message for a relationship is not being used then that is a bug. Could you create a reproduction and open an issue?

Thanks for the lightspeed response. I’ll try to create a reproduction (might take some time …)

Your statement that localization happens at the “edges” seems very reasonable. However, I’m not sure how it should be done in this case.

Suppose that I want to have a generic delete functionality in my application. If somebody tries to delete a parent record (say, a Post) with some child records (Comments) belonging to it, the message “Deleting this post would leave comments behind.” should be shown to the user.

Of course, this message should only be shown if there is a belongs_to relationship with the corresponding foreign key constraint specified in the domain model.

Now, how do I construct the message (and localize it) if somebody tries to delete a Post with Comments still attached? I only get an InvalidAttribute error with field :id, the message “would leave records behind” and private_vars (which I’m probably not supposed to use - they are private, after all). If I dare to use the private_vars, I see that a :foreign constraint is involved, and I get the constraint_name, which could be anything.

Should I resort to Introspection to get the necessary information to construct my error message? That seems to be rather awkward. Shouldn’t the error contain the information about the violation of a constraint in the domain, so that I can use it at the “edges”?

Right, so it makes perfect sense to construct error messages internally that allow you to uniquely identify errors, but the actual localization of that would happen at the edges.
With that said, adding more information like the resource, relationship etc. to errors would only be a positive thing. Something we have to incrementally improve.

Any things like that, even small things, are worth issues on GH. Even if its just “improve X error information to include resource & relationship”.

OK - thank you again. I’ll see what I can do regarding GH issues. :slight_smile:

1 Like