Reputation: 803
I would like to copy a record that has_many :pictures
. Copying the record is a no brainer, but copying the Picture
records is something else.
Picture
record has a link to a Post
Picture
has an attribute (for Dragonfly) that is image_uid
that contains a string like 2016/08/17/3chjxpz97o_tfss_05bbc7ac_a432_4408_bf6e_a0fa3dc4630d_animage.jpeg
The image is stored on an AWS S3 server. From the server perspective I think this does
image_uid
) to the serverPicture
recordimage_uid
so I can set it manually on the new record?Thx
Upvotes: 1
Views: 227
Reputation: 803
I have found how this is done. For people who might ever need this:
My original Picture
object:
#<Picture:0x007f82570f8f58> {
:id => 285,
:image_uid => "2016/10/06/6tacpx09uq_large_0.jpeg",
:number => nil,
:main => true,
:created_at => Thu, 06 Oct 2016 08:59:44 UTC +00:00,
:updated_at => Thu, 06 Oct 2016 08:59:48 UTC +00:00,
:user_id => 46,
:company_id => 27,
:public => true
}
Duplicating this is actually not that hard. I used the .dup
method provided by Ruby. Copying multiple pictures:
pictures.each do |p|
p2 = Picture.create(image:p.image, user:to_user, company:to_company, public:true, main: p.main)
end
The image:p.image
is where you do the actual image duplication.
Upvotes: 1