1

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

1 Answer 1

4

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
Sign up to request clarification or add additional context in comments.

1 Comment

Agree, there's no good reason to be adding a to_url to your attribute. IMO, that kind of meta-programming should be reserved for situations which have a really good justification.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.