Diego Zacarias
Diego Zacarias

Reputation: 793

Adding a custom method to a model attribute?

Is this any possible?

I'd like to have something like

User.avatar.to_url

which would then print the full URL address for the user's avatar image.

=> "http://url.com/images/avatars/1262694724.jpeg"

Of course, the avatar attribute would be an existing column on the users table which contains a long integer.

The to_url method im thinking on would be defined as:

def to_url
    "http://url.com/images/avatars/#{self}.jpeg"
end

Upvotes: 1

Views: 315

Answers (1)

rfunduk
rfunduk

Reputation: 30442

If avatar is an attribute (as opposed to another model/association) then you're going to save yourself a world of trouble by just doing:

def avatar_url
  "http://url.com/images/avatars/#{avatar}.jpeg"
end

Upvotes: 4

Related Questions