A Fresh Cup is Mike Gunderloy's software development weblog, covering Ruby on Rails and whatever else I find interesting in the universe of software. I'm a full-time software developer: most of my time in recent years has been spent writing Rails, though I've dabbled in many other things and like most people who have been writing code for decades I can learn new stuff as needed.

As of October 2016, I'm not actively looking for my next job, but I'm still happy to chat about potential opportunities. I'm not able to relocate, so unless you're in the Evansville area, I'd need a completely remote gig. I have lots of experience working remote. Prefer full-time but I wouldn't be averse to an interesting contact gig. Drop me a comment if you've got something or email MikeG1 [at] larkfarm.com.

« Bug Trackers I Have Known | Main | Double Shot #346 »

A Little RAILS_ROOT Tidiness

Ever write code like this in Rails?

[sourcecode language='ruby']
if File.exists?(File.join(RAILS_ROOT, 'config', 's3.yml'))
has_attached_file :s3_image,
:storage => :s3,
:s3_credentials => File.join(RAILS_ROOT, 'config', 's3.yml')

That particular snippet is from an application that optionally uses Amazon S3 to store images, if there is a configuration file for S3 supplied in the deployment. Well, you can clean that up a bit. First, although the RAILS_ROOT constant is still supported, we've had the prettier-looking Rails.root for months. Second, as of a recent commit to edge rails, Rails.root returns a Pathname object, so it directly supports the join method. Hence, the code snippet above can now be:

[sourcecode language='ruby']
if File.exists?(Rails.root.join('config', 's3.yml'))
has_attached_file :s3_image,
:storage => :s3,
:s3_credentials => Rails.root.join('config', 's3.yml')

A tiny improvement, but of such tiny improvements are a pleasant framework made.

Reader Comments (4)

Nice tip. I'd stick Rails.root.join('config', 's3.yml') in a local variable instead of repeating it.

December 5, 2008 | Unregistered CommenterMark Wilden

Pathname is IMHO one of the more useful, but most underused libraries. It makes working work files alot simpler than using the File class directy.

Although I'd probably tend to write that example using the Pathname#exists? method instead of File.exists?, like this: http://pastie.org/332348

December 5, 2008 | Unregistered CommenterDan Kubb

The nicest thing about posting code samples: instant code review. Thanks, good changes both.

December 6, 2008 | Unregistered CommenterMike Gunderloy

1. File.join() - which all developers will only understand


2. Rails.root.join() - which will only make sense to rails developers

I think its a pointless change that makes it slightly cleaner, but less understandable overall.

March 9, 2009 | Unregistered CommenterChrisR

PostPost a New Comment

Enter your information below to add a new comment.
Author Email (optional):
Author URL (optional):
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>