# Associating a default paperclip image from a different model

**URL:** <https://www.ruby-forum.com/t/associating-a-default-paperclip-image-from-a-different-model/185300>\
**Category:** Rails\
**Created:** [March 17, 2010, 6:29pm UTC](https://www.ruby-forum.com/t/associating-a-default-paperclip-image-from-a-different-model/185300 "2010-03-17T18:29:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bingobob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e480ec/32.png) [@bingobob](https://www.ruby-forum.com/u/bingobob)\
**Post date:** [March 17, 2010, 6:29pm UTC](https://www.ruby-forum.com/t/associating-a-default-paperclip-image-from-a-different-model/185300/1 "2010-03-17T18:29:38Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![bingobob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e480ec/32.png) [@bingobob](https://www.ruby-forum.com/u/bingobob)\
**Post date:** [March 17, 2010, 6:38pm UTC](https://www.ruby-forum.com/t/associating-a-default-paperclip-image-from-a-different-model/185300/2 "2010-03-17T18:38:31Z")

</div>

On 17 March 2010 17:29, bingo bob [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> attempt, I believe it’s along those lines, but not what I have!  
> What was the result of your code? Was it a syntax error or run time?

Colin

---

<div class="post-metadata">

**Author:** ![bingobob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e480ec/32.png) [@bingobob](https://www.ruby-forum.com/u/bingobob)\
**Post date:** [March 20, 2010, 3:19pm UTC](https://www.ruby-forum.com/t/associating-a-default-paperclip-image-from-a-different-model/185300/3 "2010-03-20T15:19:08Z")

</div>

Hi Colin,

Here’s the error I get with my code…

undefined method `country’ for #Class:0x10344bac0

just to reiterate here, what i’d like to do here is set the default  
image (paperclipped) to be that of the “belonged\_to” country…

bb.

---

<div class="post-metadata">

**Author:** ![bingobob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e480ec/32.png) [@bingobob](https://www.ruby-forum.com/u/bingobob)\
**Post date:** [March 20, 2010, 3:19pm UTC](https://www.ruby-forum.com/t/associating-a-default-paperclip-image-from-a-different-model/185300/4 "2010-03-20T15:19:52Z")

</div>

Just to be clear that error is generated with this code in place…

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
