-
Create the database
bundle exec rake db:create
- The name of the database is determined in
database.yml
bundle exec
is recommended to use before a command, this will execute the command using the versions specified in the Gemfile of this project (which may be different than what is installed on your machine)
-
Create a migration file to add a table to the database
bundle exec rake db:create_migration NAME=the_name_of_your_migration
- Typical name convention: “create_articles” where
articles
is the name of the table - Table names are plural (they hold multiple records, ex. “Articles”)
- Model names are singular and lowercase (represents one record in the database table, ex. “article.rb”)
- Recommended to use single word for table name otherwise things can get complicated
-
Fill in the migration file to describe the table
- Define the columns for your table
- Include database level validations
- Declare any
belongs_to
associations (the database table does not needhas_many
) - Common column types: string, text, boolean, integer
- Common database validations: null, unique
- Always include timestamps!
-
Run your migration to add it to your schema and make sure it can be rolled back!
bundle exec rake db:migrate && bundle exec rake db:rollback && bundle exec rake db:migrate
- With this command the migration will be added to the schema only if all three statements are successfully evaluated
- This ensures we can rollback the migration if needed to make changes
- Look at the schema to make sure your changes are there
- We should be starting to reference our schema when we want to see our database rather than opening up postgres in the terminal
- We now have a table in the database to store our data, but in order to add data we need two things:
- Access to a
.create
method (not provided by Ruby) - A class we can call
.create
on to represent that data as an object
- Access to a
-
Create a model for your table
- Model names are singular and lowercase (represents one record in the database table, ex. “article.rb”)
- The model should inherit from
ActiveRecord::Base
(this provides very helpful methods like.create
,.all
, etc.) - Include necessary model level validations to stay consistent with the validations in your database
- Common model validations: presence, length, numericality, uniqueness, format, inclusion
- At the model level we have more options for validations
- Include necessary model level associations
-
Create data to add to the database
- Open an interactive pry session (irb) in the terminal:
pry -r ./server.rb
- This irb session has your database configuration by requiring server.rb
- This gives us access all ActiveRecord CRUD commands
- This is a great place to play around with data, test out validations and associations, check data in our database, or add new records
- Remember, we should be moving away from viewing our database through postgres
.new
on a class will initialize a new record, does not attempt to save.new
on a class with no arguments will initialize a new record with nil values, and allows you to see the attributes it can accept.save
on an object will attempt to save it to the database, and return true or false depending if it successful.save!
(with a bang) on an object will attempt to save it to the database, and will raise an error if a validation fails (super useful when in a pry session!).create
on a class will initialize the creation of a new object (.new
) and attempt to save that object (.save
).create
is a persistence ActiveRecord method that will add a record to the database (if it passes all validations) AND it returns that object in memory for us to interact with (even if it did not persist to the database)- When something is in memory, like in our pry session, it’s temporary, but when we use persistence CRUD methods like
.create
or.update_attributes
it updates the object in memory and in the database
- Open an interactive pry session (irb) in the terminal:
Last active
April 6, 2022 11:18
-
-
Save dovakeen118/408cddf718ea912391df06f69dd26248 to your computer and use it in GitHub Desktop.
Sinatra ActiveRecord Migrations Workflow
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment