Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix documentation for Active Storage usage in Rails 6.1 #148

Open
rossatboulder opened this issue Aug 5, 2023 · 0 comments
Open

Fix documentation for Active Storage usage in Rails 6.1 #148

rossatboulder opened this issue Aug 5, 2023 · 0 comments

Comments

@rossatboulder
Copy link

When cloning models with ActiveStorage attachments, I was encountering errors like this:
Errno::ENOENT (No such file or directory @ rb_file_s_size - /tmp/ActiveStorage
when using code like this:

# Rails 6
pirate.deep_clone include: :parrot do |original, kopy|
  if kopy.is_a?(Pirate) && original.avatar.attached?
    original.avatar.open do |tempfile|
      kopy.avatar.attach({
        io: File.open(tempfile.path),
        filename: original.avatar.blob.filename,
        content_type: original.avatar.blob.content_type
      })
    end
  end
end

I fixed the errors by changing the code to:

# Rails 6.1
pirate.deep_clone include: :parrot do |original, kopy|
  if kopy.is_a?(Pirate) && original.avatar.attached?
      kopy.avatar.attach({
        io: StringIO.new(original.avatar.download),
        filename: original.avatar.filename,
        content_type: original.avatar.content_type
      })
  end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant