I have one original object post.rb with the next fields or attributes.
class Post
include Mongoid::Document
mount_uploader :posted, PostedUploader, mount_on: :posted_filename
field :posted
field :remote_posted_url
attr_accessible :posted, :remote_posted_url
end
Then I want create a copy from the first original object and share images for the new object. Then I doing:
attribs = @post.attributes.select {|a| %w(posted remote_posted_url).include? a }
new_post = Post.new attribs
new_post.save
So far, everything works fine the new object, share the images of the original. The two objects have the same shared image with the same path.
The problem is if I delete the original object, the clone object can not find the image because I deleted in original post.
How can I, in my destroy action object, with a callback before_destroy, check whether the image is used by one or more objects.
if the image, using 2 or more objects,
Not delete the image.
otherwise, if the image is used by a single object,
Delete the image.
Its possible?
I’m need a method to verify this problem.
Override the
remove_postedcallback injected by CW in your model:PS:
Post.new(attribs).saveshould be copying the file, not use the same one, maybe you ought to assign a File.open(path) to the posted column insted of the uploader.