Skip to content

has_many dupes do not set belongs_to in memory #93

Description

@adsteel

Given the following models:

class User
  has_many :posts

  amoeba do
    enable
    include_association :posts
  end
end

class Post
  belongs_to :user, optional: false
end

When I attempt to amoeba dupe a user, it will fail

> user.amoeba_dup.save!
ActiveRecord::RecordInvalid: Validation failed: User must exist

This is because amoeba sets only user.posts in memory, but not post.user and the presence validator is run before anything is saved.

The following fixes the issue:

class User
  has_many :posts

  amoeba do
    enable
    include_association :posts

    customize(lambda { |_, new_user|
      new_user.posts.each do |post|
        post.user = new_user
      end
    })
  end
end

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions