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
Given the following models:
When I attempt to amoeba dupe a user, it will fail
This is because amoeba sets only
user.postsin memory, but notpost.userand the presence validator is run before anything is saved.The following fixes the issue: