rails: accepts_nested_attributes_for breaks the uniqueness with scope validation

Hi All, I think for the following configuration accepts_nested_attributes_for + validation seems to be broken!! Rails 4.2.1 Ruby 2.1.3

class Country < ActiveRecord::Base
  has_many :cities, dependent: :destroy
  accepts_nested_attributes_for :cities
end

class City < ActiveRecord::Base
  belongs_to :country
  validates :language , uniqueness: { scope: :country_id }
end

country = Country.new(description:'abc', language: 'en-US', cities:[City.new(language: 'en-US', text: 'abc'), City.new(language: 'en-US', text: 'abc')])
country.save!

works!!

c1 = City.new(language: 'en-US', text: 'abc', country_id: 1)
c1.save!

c2 = City.new(language: 'en-US', text: 'xyz', country_id: 1)
c2.save! #does not work

Here is executable report https://gist.github.com/spurnaye/0ab33d62dc1f12751ee0 Appreciate efforts on this!

About this issue

  • Original URL
  • State: open
  • Created 9 years ago
  • Reactions: 61
  • Comments: 75

Commits related to this issue

Most upvoted comments