• J
    Prevent has_one from touching parent record unless persisted · ba3ef762
    Josh 提交于
    Previously, if `build_association` was called multiple times for a `has_one` association but never committed to the database, the first newly-associated record would trigger `touch` during the attempted removal of the record.
    
    For example:
    
        class Post < ActiveRecord::Base
          has_one :comment, inverse_of: :post, dependent: :destroy
        end
    
        class Comment < ActiveRecord::Base
          belongs_to :post, inverse_of: :comment, touch: true
        end
    
        post = Post.create!
        comment_1 = post.build_comment
        comment_2 = post.build_comment
    
    When `comment_2` is initialized, the `has_one` would attempt to destroy `comment_1`, triggering a `touch` on `post` from an association record that hasn't been committed to the database.
    
    This removes the attempt to delete an associated `has_one` unless it’s persisted.
    ba3ef762
has_one_associations_test.rb 26.2 KB