Right now I have a Rails 3 model for storing profile data. One of the columns in that database table contains an image url if the user chooses to display a profile picture (this also integrates with the Facebook Graph API to store the user’s profile picture url if they login with Facebook but that’s irrelevant). The problem I am having is that when the image column is nil, I need a way to set it to a default path on my server. Please note that I cannot use a default value in a migration or model here. My thought was to use an after_find but the following is not working:
In Profile Model:
def after_find
if self.image.nil?
self.image = "/assets/generic_profile_image.png"
end
end
In view (HAML):
.profile_pic
= image_tag @user.profile.image
The Profile model is linked to a User model via a has_one association. Right now instead of dynamically turning the image attribute into “/assets/generic_profile_image.png”, it seems to do nothing leaving me with the following generated code on my page:
<div class='profile_pic'>
<img alt="Assets" src="/assets/" />
</div>
Any suggestions on how to fix this issue would be greatly appreciated!
P.S. A conditional in the view is out of the question as the profile image is shown in way too many places!
My guess is your after_find callback ins’t actually getting called. You need to define it this way:
Now everything should work fine.