Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Fetching contributors…
Cannot retrieve contributors at this time
58 lines (43 sloc) 2.56 KB
Stubs out a new model. Pass the model name, either CamelCased or
under_scored, and an optional list of attribute pairs as arguments.
Attribute pairs are column_name:sql_type arguments specifying the
model's attributes. Timestamps are added by default, so you don't have to
specify them by hand as 'created_at:datetime updated_at:datetime'.
You don't have to think up every attribute up front, but it helps to
sketch out a few so you can start working with the model immediately.
If you include an attribute with a type of :references, :belongs_to,
:has_many, :has_one, or :has_and_belongs_to_many, the model will contain
the appropriate association declaration.
You can also decorate a type with suffixes that specify further code
generation in the model:
Adding validations using the +v token:
title:string+vp => validates_uniqueness_of :title
username:string+vl => validates_length_of :username
username:string+vl5 => validates_length_of :username, :minimum => 5
rating:float+vn => validates_numericality_of :rating
hits:integer+vi => validates_numericality_of :hits, :only_integer => true
name:string+vu => validates_uniqueness_of :name
name:string+vuc => validates_uniqueness_of :name, :case_sensitive => false
password:string+vc => validates_confirmation_of :password and
validates_presence_of :password_confirmation
accessors too:
title:string+aa => attr_accessor :title
password:string+ap => attr_protected :password
You can also use several tokens at once:
username:string+vl6+vu => generates validates_length_of :username and validates_uniqueness_of :username
A side from validations, you can add other options to your attributes:
hits:integer+vi+cd0 => validates_numericality_of :hits, :only_integer => true and in the migration t.hits, :integer, :default => 0
name:string+cn => :string, :null => false
This generates a model class in app/models, a unit test in test/unit,
a test fixture in test/fixtures/singular_name.yml, and a migration in
`./script/generate model_plus account`
creates an Account model, test, fixture, and migration:
Model: app/models/account.rb
Test: test/unit/account_test.rb
Fixtures: test/fixtures/accounts.yml
Migration: db/migrate/XXX_add_accounts.rb
`./script/generate model_plus post title:string body:text published:boolean`
creates a Post model with a string title, text body, and published flag.
Jump to Line
Something went wrong with that request. Please try again.