How to limit uniqueness scope to parent while creating nested attributes?

Example -

class Group < ApplicationRecord
 has_many :options
 accepts_nested_attributes_for :options

class Option < ApplicationRecord
 belongs_to :group
 validates :name, uniqueness: { scope: :group_id }

If I try to create a new group, I don’t want to create duplicate options with same name for the same group. Other groups may have options with same name.

age: 10,
option_attributes: [
{name: "Red"},
{name: "Red"},

Is there a way to do this in Rails ?

I don’t want to raise duplication errors, I want to create a unique option silently.

Certainly! In the given example of a Rails application, you can limit the uniqueness scope to the parent group while creating nested options by overriding the options_attributes= method within the Group class. By applying the uniq method on the attributes based on the option’s name, it ensures that duplicate options are filtered out for a specific group. This customization allows for the creation of unique options silently within a group, without raising duplication errors, while still allowing other groups to have options with the same name.