Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9197799
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:08:56+00:00 2026-06-17T22:08:56+00:00

I’ve got the following (sanitized) models: class Person < ActiveRecord::Base attr_accessible :name, :first_name, :last_name,

  • 0

I’ve got the following (sanitized) models:

 class Person < ActiveRecord::Base

      attr_accessible :name, :first_name, :last_name, :age, :job_title, :salary, :ssn, :prison_convictions, :addresses_attributes

      has_many :addresses, inverse_of: :person

      accepts_nested_attributes_for :addresses, allow_destroy: true

 end



 class Address < ActiveRecord::Base

      attr_accessible :zip_code, :street,:house_number,
 :unique_per_person_government_id

      belongs_to :person, inverse_of: :addresses

      validates_uniqueness_of :unique_per_person_government_id, scope: :person_id
 end

The problem is as follows,

Lets say Person Joe Shmoe has two addresses currently attached to himself

666 Foo Street, 12345 with unique id: “ABCDEFG”
and
777 Lucky Avenue, 54321 with unique id: “GFEDCBA”

And lets say that the following post comes through from a form:

 {:addresses_attributes =>
 { [0] => {:unique_per_person_government_id => “ABCDEFG”, :street=> “Foo Street”, :house_number => 666, :zip_code=>12345, _destroy => 1}
 [1] => {:unique_per_person_government_id => “ABCDEFG”, :street=>”Bar Street”, :house_number => 888, :zip_code => 12345, _destroy => 0}
 }

The behavior appears to be that the second (ie the new) record is validated for uniqueness first, failing validation.

The behavior I would like is to quite simply remove all elements that are marked_for_destruction? before conducting any further validation.

How can I rewire the lifecycle in this way? Is there a better way to achieve this?

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T22:08:57+00:00Added an answer on June 17, 2026 at 10:08 pm

    I’ve solved this as follows:

    class Person < ActiveRecord::Base
      attr_accessible :name, :first_name, :last_name, :age, :job_title, :salary, :ssn, :prison_convictions, :addresses_attributes
    
      has_many :addresses, inverse_of: :person
    
      accepts_nested_attributes_for :addresses, allow_destroy: true
    
      def validate_addresses
        codes = {}
        addresses.each do |a|
          if codes.key?(a.unique_per_person_government_id) and not a.marked_for_destruction?
            codes[a.unique_per_person_government_id] = codes[a.unique_per_person_government_id]+1
            if codes[a.unique_per_person_government_id] > 1
              return false
            end
          elsif not codes.key?(a.unique_per_person_government_id) and a.marked_for_destruction?
            codes[a.code] = 0
          elsif not codes.key?(a.unique_per_person_government_id) and not a.marked_for_destruction?
            codes[dacode] = 1
          end
        end
        return true
      end
    end
    
    
    class Address < ActiveRecord::Base
      before_save :validate_addresses
    
      attr_accessible :zip_code, :street,:house_number, :unique_per_person_government_id
    
      belongs_to :person, inverse_of: :addresses
    
      validates_uniqueness_of :unique_per_person_government_id, scope: :person_id, unless: :skip_validation?
    
      def skip_validation?
        person.addresses.each do |a|
          if unique_per_person_government_id == a.code and id != a.id
            return false unless a.marked_for_destruction?
          elsif id == a.id and a.marked_for_destruction?
            return false
          end
        end
        return true
      end
    end
    

    Thus enforcing uniqueness, and preventing a person with invalid addresses from saving, but ignoring items that are marked for destruction. If anybody has had a similiar problem and has a better/algorithmically simpler/easier to read solution, and would like to share it, that would be awesome 😀

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I've got a string that has curly quotes in it. I'd like to replace
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
i got an object with contents of html markup in it, for example: string
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.