SublimeText2 Rails Snippet for Model Find Code

If you find yourself typing these kind of similar codes very often, and want a snippet to save your time, this is it.

@topic = Topic.find(params[:topic_id])
@car_item = CarItem.find(params[:car_item_id])
@user = User.find(params[:id])

It maps the variable name to its model name by converting the first character or the character after a underscore to uppercase, which is a convention in Rails framework.

Demo Video

The Snippet

I put it and named as SublimeText/Packages/User/Snippets/model_find.sublime-snippet.

@${1} = ${1/^(.)|_(.)/\U\1\2/g}.find(params[:${2:${1}_}id])

So now I just have to hit mf -> tab to trigger it.

SublimeText Document

Here I'm using the Substitutions feature of SublimeText 2 snippet.

The substitution syntax has the following syntaxes:

  • ${var_name/regex/format_string/}
  • ${var_name/regex/format_string/options}


Original: ${1:Hey, Joe!}
Transformation: ${1/./=/g}

# Output:

      Original: Hey, Joe!
Transformation: =========
November 25, 2012 SublimeText

Share on Twitter Share
Qihuan Piao

Qihuan Piao

(aka kinopyo) is Chinese based in Tokyo. Software writer. He shares stories inspired him in this blog. His infamous line - "I feel calm when I kill those monsters, or people (in game)" shocks his friends deeply.
He also writes in Japanese and Chinese.