Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
travisjeffery committed Oct 4, 2019
1 parent d830c1a commit 1b578a3
Showing 1 changed file with 27 additions and 52 deletions.
Expand Up @@ -6,70 +6,45 @@ comments: false
collection: rails
---

Rails's generators can be pretty cool, and by knowing some simple tricks
you can save yourself from tedious work:
Rails' generators can pump out code and save you from tedious work.

`rails g model person name:string age:integer`
This command generates a person model with a name and age:

Rails developers should probably know that this will generate a model
containing name and age fields, with string and integer data types
respectively.
rails g model person name:string age:integer

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:
When you generator a decimal field you can specify the precision --- the total number of digits in the number, and the scale --- the number of digits following the decimal point.

`rails g model book price:decimal{1,2}:index`
rails g model book price:decimal{5,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.
When you generate a migration, by following Rails' migration name conventions, Rails will generate the fields. For example, this command generates the following migration:

Things can get even more magical. By naming your migrations in the form of:
rails g migration add_body_and_pid_to_people body:string:index pid:integer:uniq

`add/remove column [and ...] to/from table`
class AddBodyAndPidToPersons < ActiveRecord::Migration
def change
add_column :persons, :body, :string
add_index :persons, :body

Rails will generate your migration and add in the change you intend to make.
add_column :persons, :pid, :integer
add_index :persons, :pid, :unique => true
end
end

So let's say we want to add an indexed body and unique person id to the people
table:
You should always check the generated migration though, it's not perfect. The migration generated by this command should remove the index as well.

`rails g migration add_body_and_pid_to_people body:string:index pid:integer:uniq`
rails g migration remove_body_from_people body:string:index

Will generate this migration:
However, the generated migration does not remove the index.

``` ruby
class AddBodyAndPidToPersons < ActiveRecord::Migration
def change
add_column :persons, :body, :string
add_index :persons, :body
class RemoveBodyFromPeople < ActiveRecord::Migration
def up
remove_column :people, :body
end

add_column :persons, :pid, :integer
add_index :persons, :pid, :unique => true
end
end
```
def down
add_column :people, :body, :string
end
end

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`

``` ruby
class RemoveBodyFromPeople < ActiveRecord::Migration
def up
remove_column :people, :body
end

def down
add_column :people, :body, :string
end
end
```

Notice that the specified index will not be added or removed.

Edit: I fixed this issue and it's [merged in Rails](https://github.com/rails/rails/commit/b2a59388b2ad281ccce1f72dd5fda09ca746dc32).
Update: [I fixed this issue Rails](https://github.com/rails/rails/commit/b2a59388b2ad281ccce1f72dd5fda09ca746dc32).

0 comments on commit 1b578a3

Please sign in to comment.