-
Notifications
You must be signed in to change notification settings - Fork 21.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
validates_uniqueness_of
and accepts_nested_attributes_for
only validates existing records
#4568
Comments
Looks like a duplicate of #1572 . that may help you know whats happening. |
Okay, but that doesn't actually specify a solution... |
This is currently an active issue:
|
+1 |
3 similar comments
+1 |
+1 |
+1 |
+1 ! ! ! |
+1 |
Still present in Rails 3.2.8, +1 |
Here's the hacked solution I came up with, that allowed me to put the error on the failed attribute itself, and not on the base of the parent object as the solution floating around here suggests: http://stackoverflow.com/questions/2772236/validates-uniqueness-of-in-nested-model-rails#answers-header class Event < ActiveRecord::Base
has_many :vendors, dependent: :destroy
before_validation do
vendors.map do |e|
stream_ids = vendors.collect(&:audio_stream_id).compact
stream_ids_counted = stream_ids.inject({}) { |hash,stream_id| hash[stream_id] = (hash[stream_id] || 0) + 1; hash }
duplicate_stream_ids = stream_ids_counted.select { |k,v| v > 1 }.keys
e.instance_variable_set "@duplicate_stream_ids", duplicate_stream_ids
end
end
end class Vendor < ActiveRecord::Base
belongs_to :event
validate :ensure_unique_audio_stream_per_event
def ensure_unique_audio_stream_per_event
errors.add(:audio_stream_id, "taken") if @duplicate_stream_ids.present? and @duplicate_stream_ids.include?(audio_stream_id)
end
end So, in usage: @event = Event.first
@event.vendors << Vendor.new(audio_stream_id: 1)
@event.vendors << Vendor.new(audio_stream_id: 1)
@event.valid? #false
@event.full_messages # the :audio_stream_id attribute in the corresponding Vendor record is marked, so Formtastic & others displays the message nicely |
I opened a pull request with a fix for this bug, take a look. Let me know if you have suggestions or comments. |
👍 Wangjohn's pull request #9364 looks like it goes a long way toward fixing this issue, but nobody has even commented on it yet. |
Same problem :) |
+1 |
👍 |
1 similar comment
+1 |
…n uniqueness validation. Fixes rails#4568. Remove the validation context-related changes because they were removed from mainline in 75135a9
+1 |
Hi guys! I think it's not worth adding support for this case. It's going to change a lot of code just for a 'partial' fix. I mean partial, because it may not have database constraints. Some information about this can be found here. wdty? |
+111111 |
I agree with @laurocaetano. This is a partial fix that demands a lot of code. The proper fix, as documented is to use database constraints. |
+1 |
+infinity |
+1 seems to be an issue still in rails 4 (unless there's some easy solution that I have overlooked) |
+1 still seems to be happening to me. |
+1 |
@alagu @rderoldan1 : Please read Rafael's answer, this is not easily fixable so this issue has been marked as closed but the problem remains. |
If I have a nested model, such as:
The uniqueness validator only applies to existing records in the database... If I submit the same duplicate record as a nested attribute:
It will add all those records, even though the
phone
is supposed to be unique...The text was updated successfully, but these errors were encountered: