There are some very useful methods that operate on the names of your models, tables and controllers in Rails. I use them infrequently enough that I have to relearn them every time - so I'm putting them in here:
Given a string with the name of a model (e.g. Project), how can I create an ActiveRecord call on the fly using that model?
str = 'Project'
project = (str.constantize).find(1)
How can I get the name of the corresponding controller ?
controller = str.tableize
Given a string with the name of a controller, how can I get the name of the Class/Model ?
str = 'sales_contact'
model_name = str.camelize
I the string is plural:
str = 'sales_contacts'
model_name = str.classify
How do I do the reverse operation?
str = 'SalesContact'
name = str.underscore
A collection of computer systems and programming tips that you may find useful.
Brought to you by Craic Computing LLC, a bioinformatics consulting company.
Subscribe to:
Post Comments (Atom)
Contributors
Archive of Tips
-
▼
2011
(44)
-
▼
November
(11)
- strsplit in R
- Running R scripts on the Command Line
- Deleting a File that starts with '-' on UNIX
- Plotting a simple bar plot in R
- Captain Beefheart Song Titles
- When jQuery $.ajax() keeps returning the same valu...
- Updating a DIV with jQuery and Sinatra
- Latest version of jQuery on Google APIs
- Sorting on multiple String keys in Ruby
- Manipulating Model/Table/Controller Names in Rails
- Parsing 96 / 384 well Plate Maps
-
▼
November
(11)
No comments:
Post a Comment