views:

22

answers:

1

Here's some code, this is my NewsItem model as you can see...

class NewsItem < ActiveRecord::Base
  belongs_to :country
  has_attached_file :image, :styles => { :original => '57x57' }, :default_url => '/images/football.png'

  #   has_attached_file :image, 
  #                     :styles => { :original => '57x57' }, 
  #                     :default_url => self.country.flag.url

  validates_attachment_content_type :image, :content_type => ['image/png'], :message => "only pngs are allowed."
  validates_presence_of :title
  validates_length_of :title, :within => 3..50
  validates_presence_of :url
end

I have a Country model also which has a flag (paperclip attached image)...What I'd like to do is make the default image for the NewsItem be the Country flag? My commented out code shows my unsuccessful attempt, I believe it's along those lines, but not what I have!

A: 

Paperclip url/path parameters accept interpollations.

So use can define your own, for example:

  Paperclip.interpolates :country_flag_url do |attachment,style|
    attachment.instance.country.flag.url
  end

  has_attached_file :image, :styles => { :original => '57x57' }, :default_url => ':country_flag_url'
Voyta