3

There are a number of similar questions/answers on SO, but none seem to solve my issue.

My goal is to generate a 'dynamic watermark' (user avatar overlay on an another image) for an image using Paperclip. The problem I'm having is that I cannot get the 'user_id' attribute of the model to be able to grab the avatar file/url using a dynamic processor. Please note that I'm using the 'watermark.rb' processor (found elsewhere in the internets). I can successfully generate a static watermark, but attachment.instance is nil. Please see:

class NicerImage < ActiveRecord::Base
   attr_accessible :content, :image, :user_id
   belongs_to :user
   has_attached_file :image,
                     :styles => lambda { |attachment| {
                      :large => {
                          :processors => [:watermark],
                          :geometry => "800>",
                          :watermark_path => User.find(attachment.instance.user_id).avatar.url(:medium),
                          :position => 'SouthEast'
                      }
                     }
                    },
                    :storage => :s3,
                    :s3_credentials => "#{Rails.root}/config/aws.yml",
                    :bucket => Rails.configuration.s3[:general_bucket],
                    :convert_options => { :all => "-auto-orient" }
end

The error I am getting is undefined method `user_id=' for nil:NilClass (since attachment.instance is nil)

Thanks!

Sauronlord
  • 237
  • 2
  • 13

1 Answers1

0

I solved my problem by checking whether avatar_file_name.blank?.

class NicerImage < ActiveRecord::Base
 attr_accessible :content, :image, :user_id
 belongs_to :user
 has_attached_file :image,
                 :styles => lambda { |attachment| {
                  :large => {
                      :processors => [:watermark],
                      :geometry => "800>",
                      :watermark_path => User.find(attachment.instance.user_id).avatar_file_name.blank? ? nil : User.find(attachment.instance.user_id).avatar.url(:medium),
                      :position => 'SouthEast'
                  }
                 }
                },
                :storage => :s3,
                :s3_credentials => "#{Rails.root}/config/aws.yml",
                :bucket => Rails.configuration.s3[:general_bucket],
                :convert_options => { :all => "-auto-orient" }
end
Sauronlord
  • 237
  • 2
  • 13