Rails’s generators can be pretty cool, and by knowing some simple tricks you can save yourself from tedious work:
rails g model person name:string age:integer
Rails developers should probably know that this will generate a model containing name and age fields, with string and integer data types respectively.
You can even specify attribute options, let’s make a new book model with an indexed price field that needs the given precision 1 and scale 2:
rails g model book price:decimal{1,2}:index
This has slightly more magic since it’s not apparent what the command does just by looking at it, e.g. you have to know the precision/scale order.
Things can get even more magical. By naming your migrations in the form of:
add/remove column [and ...] to/from table
Rails will generate your migration and add in the change you intend to make.
So let’s say we want to add an indexed body and unique person id to the people table:
rails g migration add_body_and_pid_to_people body:string:index pid:integer:uniq
Will generate this migration:
1 2 3 4 5 6 7 8 9 | |
Pretty cool, huh?
You should always check the generated migration though, it’s not perfect.
rails g migration add_body_to_people body:string:index
Generates the migration we expect, however,
rails g migration remove_body_from_people body:string:index
1 2 3 4 5 6 7 8 9 | |
Notice that the specified index will not be added or removed.
March 21 2012 Edit: I fixed the issue above and has been merged in Rails.