database_validations icon indicating copy to clipboard operation
database_validations copied to clipboard

Using with `accepts_nested_attributes_for` does not show error message for child object

Open andreaflether opened this issue 7 months ago • 0 comments

Hello,

I started using the gem considering the Rails problem with uniqueness and accepts_nested_attributes_for, but I'm having a problem that I'm not sure it's a gem limitation or a implementation error of mine.

I have these relationships/models:

class Team < ApplicationRecord
   has_many :requested_units, index_errors: true
   accepts_nested_attributes_for :requested_units
end
class RequestedUnit < ApplicationRecord
   belongs_to :team
   validates_db_uniqueness_of :code, index_name: 'IX_REQ_UNIT_CODE', scope: %i[team_id], rescue: :always
end

I think the uniqueness itself works well since the log shows an rollback (ROLLBACK TO SAVEPOINT active_record_1), but when doing @team.errors, there is no errors to be displayed, and the object is stated as valid.

3.2.2 :020 > team = Team.new(requested_units_attributes: [{ code: 111 }, { code: 111 }])
=> #<Team:0x00007f7c5c1bf500 id: nil, name: "Test">

3.2.2 :021 > team.valid?
=> false

3.2.2 :022 > team.errors                                                                                                                                                                       
=> #<ActiveModel::Errors []>

Rails version: 7.0.7 Ruby version: 3.2.2

andreaflether avatar Jun 05 '25 19:06 andreaflether