@@ -27,6 +27,27 @@ rails c # or rails console
2727- Test model associations
2828- Execute arbitrary Ruby code in your app's environment
2929
30+ ``` bash
31+ rails c
32+ user = User.new # Create a new User
33+ user.name = " Test User" # Set the name of the User
34+ user.save # Save the User to the database
35+ user.id # Get the ID of the User
36+ user.auth_token # Get the auth_token of the User
37+ user.errors # Get the errors of the User
38+ user.errors.full_messages # Get the full messages of the errors
39+ user.destroy # Destroy the User
40+
41+
42+ User.all # Get all Users
43+ User.first # Get the first User
44+ User.last # Get the last User
45+ User.count # Get the number of Users
46+ User.find(1) # Get the User with ID 1
47+ User.find_by(name: " Test User" ) # Get the User with name "Test User"
48+ User.take # Get a random User
49+ ```
50+
3051### Rails Logger
3152
3253``` ruby
@@ -56,11 +77,14 @@ Other useful database commands:
5677``` bash
5778rails db:migrate # Run pending migrations
5879rails db:rollback # Rollback last migration
80+ rails db:rollback STEP=5 # Rollback last 5 migrations
81+ rails db:migrate:down VERSION=VERSION_NUMBER # Rollback to a specific version
5982rails db:reset # Drop & recreate database
6083rails db:setup # Create database, load schema, and initialize with seed data
6184rails db:drop db:create db:migrate # Reset database by dropping, creating, and running migrations
6285ActiveRecord::Base.connection.tables # List all database tables
6386ActiveRecord::Base.connection.columns(' users' ).map(& :name) # List all column names of the 'users' table
87+
6488```
6589
6690## Code Quality Tools
0 commit comments