Take the 2-minute tour ×

How could one get a carrierwave image with has certain condition?

For example:

  image_tag @profile.photos.first.file_url(:profile)

Returns the first image but what If I want something like

@photo = Photo.where(:attachable_id => 1, :attachable_type => "Profile", :main => true)

:main => true means this is the profile picture a user has selected.

Also on a side note,

I have this function wich is a mess:

def show_avatar_thumb(id)
    @profile = User.find(id).profile rescue nil
    image_tag @profile.photos.first.file_url(:img_96x96)
    image_tag ("/assets/avatars/img_96x96.png")

Is there a way to improve this? thx!

share|improve this question

1 Answer 1

up vote 1 down vote accepted
def show_avatar_thumb(id)
    user = User.find(id)
  rescue ActiveRecord::RecordNotFound
    image_tag user.profile.photos.where(main: true).first.file_url(:img_96x96)

I would pull where(main: true) out and put it in a model scope though.

share|improve this answer
that main: true doesent work the problem is i still have to dive into carrierwave it seems to have very vague helper methods only that way you can get the image –  Rubytastic Sep 8 '12 at 12:36

Your Answer


