Skip to content

Instantly share code, notes, and snippets.

@ndelage
Last active March 26, 2020 00:10
Show Gist options
  • Save ndelage/38e6ed0199d3baed46c1 to your computer and use it in GitHub Desktop.
Save ndelage/38e6ed0199d3baed46c1 to your computer and use it in GitHub Desktop.
Step by Step CRUD Applications

Step by Step CRUD

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