sharing images in objects with carrierwave

28 views
Skip to first unread message

hyperrjas

unread,
Feb 8, 2012, 7:48:31 PM2/8/12
to carrierwave
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.
Reply all
Reply to author
Forward
0 new messages