Is there any way to validate that a polymorphic association is only related to one item? for example, if I have a comments that are polymorphic and can be on photos, posts, etc. I want to ensure that if I am adding a comment to a posts’ list of comments, that if the comment is already associated with the post, the add will fail. (validation uniqueness error). Any ideas?
Share
So I’m guessing you have something like this:
Assuming your
Commentmodel has a couple of attributes likeauthorandbody(that you want to be unique) then you could create a custom validation in that model like this:I’ve also assumed that comments are created something like this: