About validations that require many-to-many models to be stored in intermediate tables

Asked 2 years ago, Updated 2 years ago, 71 views

For example, for many-to-many models of User and Group, there is an intermediate table User_Group.

When creating a group, how do I validate that user_group keeps at least one record, i.e., it must be linked to the user?


in group.rb validate:user_ids,presence:true
I think there is a way to describe as a candidate, but is there any other way?

ruby-on-rails ruby

2022-09-30 21:36

1 Answers

How about making the intermediate table user_group_id mandatory?

validate:user_group_id,presence:true

That's what it looks like.


2022-09-30 21:36

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.