Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 3b1d7dfec1
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 58 lines (43 sloc) 2.62 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
Description:
    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 => t.name :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
    db/migrate.

Examples:
    `./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.
Something went wrong with that request. Please try again.