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 622507
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T18:59:31+00:00 2026-05-13T18:59:31+00:00

I have a model, let’s say Attachments, that uses attachment_fu to accept file uploads

  • 0

I have a model, let’s say Attachments, that uses attachment_fu to accept file uploads from the user. I want to “deep copy” (or in Ruby-ese, deep clone) an Attachment, thus creating a completely new binary object in the “db_files” table.

I’ve found that it is not quite a solved problem yet. This blog posting:
http://www.williambharding.com/blog/rails/rails-faster-clonecopy-of-attachment_fu-images/

Shows a method that allegedly works for filesystem based storage. For db-based stores, the “deep copy” fails. A new “Attachment” is created but it uses the pre-existing db_file_id, thus performing a shallow copy.

Inside attachment_fu’s db_file_backend.rb I see the save method:

      # Saves the data to the DbFile model
      def save_to_storage
        if save_attachment?
          (db_file || build_db_file).data = temp_data
          db_file.save!
          self.class.update_all ['db_file_id = ?', self.db_file_id = db_file.id], ['id = ?', id]
        end
        true
      end

So, I am trying to decipher this and I believe “build_db_file” is some Ruby metaprogramming magic shorthand for DbFile.new although I cannot confirm this (grepping the source shows no mention of this, nor can I find it on google).

I’m not quite sure what it is doing, but my theory is that the db_file is being copied from the source obj as part of the “Deep copy” attempt (in the linked code) thus it is simply triggering a save instead of a create.

My initial theory was that the parent (Attachment) object would be set to “new” upon a deep copy attempt, thus I did something like:

 def save_to_storage
    if save_attachment?
      if self.new_record?
        db_file = DbFile.new :data => temp_data
        self.class.update_all ['db_file_id = ?', self.db_file_id = db_file.id], ['id = ?', id]
      end
    end
    true
  end

This actually works fine for cloned objects but unfortunately all the tests for regular, non cloned file uploads fail. The Attachment object is created but no data is written to db_file. Theory is that the parent object is saved first, then the db_file stuff is written later, thus new_record? returns false.

So, as an experiment I decided to try:

  def save_to_storage
    if save_attachment?
      if self.new_record?
        db_file = DbFile.new :data => temp_data
        self.class.update_all ['db_file_id = ?', self.db_file_id = db_file.id], ['id = ?', id]
      else
        (db_file || build_db_file).data = temp_data
        db_file.save!
        self.class.update_all ['db_file_id = ?', self.db_file_id = db_file.id], ['id = ?', id]
      #end
    end
    true
  end

That works partially – the db_file is populated but then I get an error on db_file.save! – saying that db_file is nil.

So, I’m sort of stymied. I can do some further trial and error but at this point I’ve hit my limited understanding of how this plugin works. I really didn’t expect or want to spend this much time on it so I am reluctant to try and explore attachment_fu any further, but I’m afraid I’m going to have to go down the rabbit hole to figure it out. Any ideas or thoughts?

Thanks!!

  • 1 1 Answer
  • 1 View
  • 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-05-13T18:59:31+00:00Added an answer on May 13, 2026 at 6:59 pm

    Okay, so instead of figuring out how to create a new db_file (which is wasteful in our particular case), I just monkey-patched destroy_file to only delete the db_file if there are no more attachment records pointing to it. This may not be appropriate if you allow someone to “modify” an attachment db_file in situ but since we don’t, this works great.

    Technoweenie::AttachmentFu::Backends::DbFileBackend.module_eval do
      protected
      def destroy_file
        if db_file && self.class.count( :conditions =>["id <> ? AND db_file_id = ?", self.id, db_file.id] ) == 0
          db_file.destroy 
        end
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have a Model, for example User, and I want to merge
Let's say that I have a model that handles recipes, and I want to
Let's say I have User model and Post model. Post model contains field user_id
Let's say I have some model objects that resemble this: public class FooModel {
Let's say i have a Domain Model that im trying to make compatible with
Let's say I have a model that looks like this: public class Blog {
Let's say I have such model class Event(models.Model) users_count = models.IntegerField(default=0) users = models.ManyToManyField(User)
Let's say that you have a Model that looks kind of like this: public
I have a model, let's say Cat , and I want to create a
Let's say I have a model called User . I have an array with

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.