Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby on Rails: Custom getter or custom helper

I want to display a default avatar image if someone has not added a photo. I'm assuming I need to either do a custom getter in the model or a helper.

If I did the getter would it look something like this:

def avatar_url
  "default_url" unless self.avatar
end
like image 531
Jhorra Avatar asked Dec 02 '25 04:12

Jhorra


1 Answers

Use the read_attribute method to detect the presence.

def avatar
  read_attribute("avatar") || default_avatar
end
like image 146
Harish Shetty Avatar answered Dec 03 '25 17:12

Harish Shetty