You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The children are invalid, so there should be an error on parent.
Actual behavior
There is no error on parent, only on the second child:
parent=Parent.new(children: [Child.new(name: 'Wiske'),Child.new(name: 'Wiske')])parent.valid?# true# Child Exists (0.6ms) SELECT 1 AS one FROM `children` WHERE `children`.`name` = BINARY 'Wiske' LIMIT 1# Child Exists (0.5ms) SELECT 1 AS one FROM `children` WHERE `children`.`name` = BINARY 'Wiske' LIMIT 1parent.save# true# (0.6ms) BEGIN# Child Exists (0.6ms) SELECT 1 AS one FROM `children` WHERE `children`.`name` = BINARY 'Wiske' LIMIT 1# Child Exists (0.5ms) SELECT 1 AS one FROM `children` WHERE `children`.`name` = BINARY 'Wiske' LIMIT 1# Parent Create (0.5ms) INSERT INTO `parents` (`created_at`, `updated_at`) VALUES ('2018-05-20 19:14:06', '2018-05-20 19:14:06')# Child Exists (0.6ms) SELECT 1 AS one FROM `children` WHERE `children`.`name` = BINARY 'Wiske' LIMIT 1# Child Create (7.2ms) INSERT INTO `children` (`name`, `parent_id`) VALUES ('Wiske', 5)# Child Exists (0.6ms) SELECT 1 AS one FROM `children` WHERE `children`.`name` = BINARY 'Wiske' LIMIT 1# (2.8ms) COMMITparent.errors.any?# falseparent.children# [#<Child id: 6, name: "Wiske", parent_id: 5>, #<Child id: nil, name: "Wiske", parent_id: 5>]parent.children[1].errors.any?# trueparent.children[1].errors.full_messages# ["Name has already been taken"]parent.valid?# false# Child Exists (0.7ms) SELECT 1 AS one FROM `children` WHERE `children`.`name` = BINARY 'Wiske' LIMIT 1parent.errors.any?# trueparent.errors.full_messages# ["Children is invalid"]
Since there are no uniqueness problems between each child individually and what's already in the database, validation of the parent passes. Each child is revalidated before being saved, so when the second child is saved, the duplication is detected and an error is added to the child model. However, this error does not propagate to the parent as typically happens with validation errors.
Steps to reproduce
Models:
Code:
Executable test
Expected behavior
The children are invalid, so there should be an error on
parent
.Actual behavior
There is no error on
parent
, only on the second child:Since there are no uniqueness problems between each child individually and what's already in the database, validation of the parent passes. Each child is revalidated before being saved, so when the second child is saved, the duplication is detected and an error is added to the child model. However, this error does not propagate to the parent as typically happens with validation errors.
System configuration
Rails version: 5.2.0
Ruby version: 2.5.1p57 (2018-03-29 revision 63029) [x86_64-darwin17]
(See also #32939)
The text was updated successfully, but these errors were encountered: