Rename image using associated model - Paperclip

三世轮回 提交于 2019-12-24 00:59:09

问题


Code

In my image model:

has_attached_file :pic
before_post_process :rename_pic
before_save ->{ p 'before_save ----------------' }
after_post_process ->{ p 'after_post_process --------------' }

  def rename_pic
    p 'en imagen'
    p self
    p 'en imagen'
  end

In service that has many images:

  # don't use accepts_nested_attributes_for
  before_save :create_images
  attr_accessor :images_attributes

  def create_images
    # images_attributes example value: { "0"=> {img_attrs}, "1" => {img_attrs1} } 
    images_attributes.all? do |k, image_attrs|
      if image_attrs.delete(:_destroy) == "false"
        p 'asd'
        image = Image.new image_attrs.merge(service_id: id)
        p image.service
        p image.service_id
        image.save
      end
    end
  end

This is the output I get:

"asd"
"en imagen"
#<Image id: nil, service_id: nil, pic_file_name: "Screen_Shot_2013-04-07_at_5.18.03_PM.png", pic_content_type: "image/png", pic_file_size: 16041, pic_updated_at: "2013-07-30 22:58:46", created_at: nil, updated_at: nil, user_id: nil>
"en imagen"
G"after_post_process --------------"
#<Service id: 427, event_id: nil, min_capacity: nil, max_capacity: nil, price: #<BigDecimal:7fb6e9d73d48,'0.0',9(18)>, image_path: nil, name: "Super Franks", desc: "zxc", created_at: "2013-05-12 19:01:54", updated_at: "2013-07-30 19:32:48", address: "pasadena", longitude: 77.225, latitude: 28.6353, gmaps: true, city: "san francisco", state: "california", country_id: "472", tags: "Banquet", created_by: 22, avg_rating: #<BigDecimal:7fb6efdbcf10,'0.0',9(18)>, views: 27, zip_code: "", address2: "", price_unit: "", category_id: 3, featured: true, publish: true, slug: "banquet-super-franks", discount: nil, currency_code: "USD", video_url: "http://www.youtube.com/watch?v=A3pIrBZQJvE", short_description: "">
427
"before_save ----------------"

Problem

When calling

image = Image.new image_attrs.merge(service_id: id)

Paperclips seems to start processing, and then set service_id.

So when I try to use service inside rename_pic service is nil.

Any ideas on how to handle this?


回答1:


This solved my problem, I changed:

before_post_process :rename_pic

to:

before_create :rename_pic

and this is rename_pic, for the record:

def rename_pic
  extension = File.extname(pic_file_name).downcase
  self.pic.instance_write :file_name,
    "#{service.hyphenated_for_seo}#{extension}"
end

where service has_many images, and image belongs_to service.




回答2:


Be carefull with the fix of @juanpastas, because if you change before_post_process to before_create, it will only run when you create your image, and not when you update it. To have the callback still run on update, do this:

class YourImage
  has_attached_file :pic

  # use both callbacks
  before_create                     :rename_pic
  before_post_process               :rename_pic

  def rename_pic
    # assotiated_object is the association used to get pic_file_name
    return if self.assotiated_object.nil?

    extension = File.extname(pic_file_name).downcase
    self.pic.instance_write :file_name,
      "#{service.hyphenated_for_seo}#{extension}"
  end
end


来源:https://stackoverflow.com/questions/17959065/rename-image-using-associated-model-paperclip

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!