Skip to content

Instantly share code, notes, and snippets.

@rafbm
Created June 24, 2012 18:07
Show Gist options
  • Save rafbm/2984232 to your computer and use it in GitHub Desktop.
Save rafbm/2984232 to your computer and use it in GitHub Desktop.
Would any Rails developer consider this bad practice?

Would any Rails developer consider this bad practice?

If so, why?

I am overriding the external_url= setter to ensure URLs start with http://. I’m doing this before any validation because I don’t want to bother the user who enters example.com/foo/bar without http://. I don’t plan on validating the URL at all anyway.

def external_url=(url)
  if !url.match /^https?:\/\//i
    url = "http://#{url}"
  end
  self[:external_url] = url
end
@allaire
Copy link

allaire commented Jun 24, 2012

Looks legit! You use attr_accessor when you don't need to change the default behavior of the getter/setter. Don't forget to protect against mass assigment (http://railscasts.com/episodes/26-hackers-love-mass-assignment-revised) :)

@rafbm
Copy link
Author

rafbm commented Jun 25, 2012

Yeah of course! Thanks for your thoughts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment