Home Copy images from another model using carrierwave[Rails]
Reply: 0

Copy images from another model using carrierwave[Rails]

DinhNgocHien
1#
DinhNgocHien Published in 2017-12-05 08:49:23Z

I have models like this: session:

class Session < ActiveRecord::Base
  belongs_to :user
  has_many :images,
           -> { order(id: :asc) },
           as: :photoable,
           class_name: "SessionImage",
           dependent: :destroy
end

user:

class User < ActiveRecord::Base
  has_many :sessions
  has_many :images,
           -> { order(id: :asc) },
           as: :photoable,
           class_name: "UserImage",
           dependent: :destroy
end

image:

class ImageManagement < ActiveRecord::Base
  mount_uploader :image, ApplicationUploader

  ################
  # ASSOCIATIONS
  ################
  belongs_to :photoable, polymorphic: true

end

Currently, I want to copy the user's images to session. I tried with this:

session.images = session.user.images

but Rails throwed the error:

ActiveRecord::AssociationTypeMismatch (SessionImage(#70057227322080) expected, got UserImage(#70057296575800))

So how can I copy images from user to session? I'm using carrierwave gem to upload image.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.331413 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO