Helpful Rails 4 commands

When creating a table and limiting a column string size

rails g scaffold users email:string{50}

To set columns in a table to not nullable or set the default value, after the migration file is created, manually open the file and edit the column you want.

t.string :column1, null:false, default: ‘ABC’

For Join tables via migration

rails g migration CreateJoinTable[Model1Model2] Model1 Model2

The above creates a table with two column model1_id and model2_id.  I’ve experienced that the table name will be Model1_Model2. Rails documentation seems to say different but that’s what I got.

If you want to redo your last migration

rake db:migrate:redo

rake db:migrate:redo STEP=2  # redo last two steps

This was helpful because I had deleted a table rails created and couldn’t figure out how to recreate it until I cam across this

If you created a migration you want to delete

rails db:migrate:status

Find the version before the one you want to delete

rake db:migrate VERSION=XXXXX

Manually delete the migration you don’t want

 

References:

http://stackoverflow.com/questions/14886156/rails-generate-scaffold-with-table-options

http://www.packtpub.com/article/Working-with-Rails-ActiveRecord-Migrations-Models-Scaffolding-and-Database-Completion 

 

Advertisements
This entry was posted in Uncategorized and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s