D
D
Denis Savitsky2012-09-29 15:33:16
Ruby on Rails
Denis Savitsky, 2012-09-29 15:33:16

Is it possible in CarrierWave to store the original image in one model, and the version in another?

Is it possible in CarrierWave to store the original image in one model, and the version in another?
And how to transfer the saved image to the second model?
Example:

class Photo < ActiveRecord::Base<br>
  attr_accessible :image, :remote_image_url<br>
  has_many :crops<br><br>
  mount_uploader :image, ImageUploader<br>
end

class Crop < ActiveRecord::Base<br>
  attr_accessible :preview, :x, :y, :w, :h<br>
  belongs_to :photo<br><br>
  mount_uploader :preview, PreviewUploader<br><br>
  before_save :crop<br><br>
  def crop<br>
    preview.recreate_versions! if x.present?<br>
  end<br>
end

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Denis Savitsky, 2012-09-29
@qweewq

It turns out that everything is simple:

class Crop < ActiveRecord::Base
  attr_accessible :preview, :x, :y, :w, :h
  belongs_to :photo
  mount_uploader :preview, PreviewUploader
  
  # здесь была ошибка - если before_save, то картинки не разделяются на папки по model.id
  after_save :crop
  
  def crop
    preview.recreate_versions! if x.present?
  end
end

To create a Crop to a Photo:
photo = Photo.find(1)
photo.crops.create(x:1,y:1,w:1,h:1,preview: photo.image) 

G
Georgy Khromchenko, 2012-09-29
@Mox

Maybe the question is not in the topic, but what is the point of using and the appearance of CarrierWave, when there was Paperclip for a long time?
I just see that people periodically use it, and from my point of view always - well, just more hemorrhoids, that's all.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question