Skip to content

Instantly share code, notes, and snippets.

@KaraAJC
Forked from ndelage/crud_step_by_step.md
Last active August 29, 2015 14:15
Show Gist options
  • Save KaraAJC/5c6d62f34c06f15db466 to your computer and use it in GitHub Desktop.
Save KaraAJC/5c6d62f34c06f15db466 to your computer and use it in GitHub Desktop.

Step by Step CRUD

##Commands to remember:

  1. Bundle Install
  2. BE rake -T
  3. BE rake console # Start IRB with application environment loaded
  4. rake db:create # Create the databases at hr-sinatra-refactor-challenge_development
  5. rake db:drop # Drop the database at hr-sinatra-refactor-challenge_development
  6. rake db:migrate # Migrate the database (options: VERSION=x, VERBOSE=false, SCOPE=blog)
  7. rake db:reset # Drop, create, and migrate the database
  8. rake db:rollback # rollback your migration--use STEP=number to step back multiple times
  9. rake db:seed # Populate the database with dummy data by running db/seeds.rb
  10. rake db:test:prepare # Migrate test database
  11. rake db:version # Returns the current schema version number
  12. rake generate:migration # Create an empty migration in db/migrate, e.g., rake generate:migration NAME=create_tasks
  13. rake generate:model # Create an empty model in app/models, e.g., rake generate:model NAME=User
  14. rake generate:spec # Create an empty model spec in spec, e.g., rake generate:spec NAME=user
  15. BE shotgun

Understand the Domain

  1. Discuss the domain (with others or yourself).
  2. Write down the user stories (features that you plan to support).
  3. Develop an understand of how each noun (model/table) will relate to each other.

Schema

  1. Draw the schema. Don't name any join tables as the combination of two other tables, find a unique noun.
  2. Double check your schema for any columns that don't follow convention (e.g. foreign keys should end in _id).
  3. Ensure you've chosen the most appropriate type (integer, string, boolean, etc) for each column.

Migrations

  1. Create the migrations for each table.
  2. Check that everything that matches your schema design.
  3. Migrate (note, you don't need any models defined to migrate!)

Models

  1. Create the ActiveRecord models.
  2. Verify your models are in good shape by saving an instance of each to the database. (via rake console).

Associations

  1. Add one or two associations to your models at a time. Don't do them all at once.
  2. As you add each association, update your seeds.rb to make use of the association. Be sure to use the bang versions (create! and save!) so any errors are printed when you run rake db:seed. Consider seeds.rb your scratch pad as you develop & test your associations.
  3. Review your seeds.rb and make sure you aren't manually setting any _id attributes. Set the association instead (president instead of president_id). Also, don't "guess" any values for an foreign key. If you need a random User, pick a random one with User.all.sample.
  4. Jump back and forth between dropping, migrating, seeding and the console to verify everything. Keep a checklist so you can be sure you've tested & verified each association is working like you expect.

Seeding

  1. By now your seeds.rb might be a bit messy since you used it as a scratch pad. Review seeds.rb and make sure you've got a decent set of data to test you application once you start creating the web interface. Now might be a good time to review your user stories/features.

CRUD

Now it's time to create the routes & views for your app. Start with the easiest to the most difficult. Be sure to follow the RESTful routing conventions. Nearly every URL should include the plural form of the model (/comments/...).

Routes & views that display data (index & show) are going to be very helpful for testing that your app is working, so be sure you get those working first -- and working well.

Repeat the following for each model you plan on exposing CRUD functionality for (only create the routes & view for the CRUD actions you need!):

  1. Create a new controller file (app/controllers/plural_model_name.rb)
  2. Create a view folder (app/views/plural_model_name)
  3. Create the index route & view
  4. Create the show route & view
  5. Create the new route & view + the create route
  6. Create the edit route & view + the update route
  7. Create the delete route.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment