jeudi 23 janvier 2020

Rails: Unique validation with nested attributes

Table 1 - Product

has_many :widget_groups
accepts_nested_attributes_for :produts_widget_groups

validates_uniqueness_of :name, on: :create,
                                     if: proc { |product| product.has_category? }

Table 2 - ProductsWidgetGroup

belongs_to :product
belongs_to :widget_group
has_many :products_widget_group_deatils

Table 3 - ProductsWidgetGroupDetail

belongs_to :products_widget_group
belongs_to :product
accepts_nested_attributes_for :product

I have a form, which I am using to save the product in (table 1) and the sending the nested attributes for table 2 and table 3.

Here I am trying to put the unique name validation for product which we are saving through the table 3 but I have some issue with that.

I need to get the model validation error message for the product object of table 1 then only I can show them the error in the frond end. But here the validation error message going to the product object of table 3. Is there anyway we can achieve that?

Aucun commentaire:

Enregistrer un commentaire