Sara Trice
Just a programmin', bellydancin', cake bakin' kinda girl

  • RSS-Feed posts
  • RSS-Feed comments

  • gallery
  • livejournal
  • résumé
  • website portfolio

Pages

  • gallery
  • livejournal
  • résumé
  • website portfolio

Fun Stuff

  • My Amazon Wish List

Webcomics

  • Dark Legacy Comics
  • Erfworld
  • Questionable Content

New Posts

  • Indexing users that belong to groups with ancestry and thinking sphinx
  • Avoiding Ruby hash conditionals in Ruby on Rails
  • Quickie: How to add a blank option to options_from_collection_for_select
  • Using javan/whenever on Engine Yard Cloud
  • Illinois Home Bakeries – for farmer’s markets only

Latest Comments

  • 08.05 | Marc-André Lafortune in Testing CKEditor in Ruby on Rails with Cucumber/Ca…
  • 01.05 | Charles in ImageMagick: "convert: color profile operates on a…
  • 25.02 | Stanislaw in Testing CKEditor in Ruby on Rails with Cucumber/Ca…
  • 24.02 | admin in Avoiding Ruby hash conditionals in Ruby on Rails
  • 24.02 | Pam in Avoiding Ruby hash conditionals in Ruby on Rails
February 2012
S M T W T F S
« Dec   Mar »
 1234
567891011
12131415161718
19202122232425
26272829  
9
Feb

Avoiding Ruby hash conditionals in Ruby on Rails

This gets really old:

if params[:teacher] && params[:teacher][:id] ...

so instead, do this:

if params[:teacher].try(:[], :id)

or do it a lot more:

name = params[:company][:owner][:name] if params[:company] and params[:company][:owner] and params[:company][:owner][:name]

turns into:

name = params.try(:[], :company).try(:[], :owner).try(:[], :name)

Yay for Stack Overflow!

by admin in tech
2 comments

Meta

  • Log in
© 2008 - 2012
Design & CSS by
Freizeitler

Blogroll

    Dark Legacy Comics Yay MMORPGs!
    Erfworld Rob Balder writes this
    My Amazon Wish List Buy me somethin’!
    Questionable Content Am I more like Faye or Dora?