Permalink
Browse files

Adding more console logs from lesson, adding steps to update database

and add data to rails project
  • Loading branch information...
danman01 committed Jul 18, 2018
1 parent 0aaebfb commit 621b4100ed52da5d8e6733bee7b12554e22ee24c
Showing with 228 additions and 2 deletions.
  1. +228 −2 rails-api-many-to-many/README.md
@@ -32,6 +32,27 @@ By the end of this, developers should be able to:
- Creating `loans` join table is [here](https://git.generalassemb.ly/ga-wdi-boston/rails-api-many-to-many/blob/master/docs/library_joins.md)
- Adding relationship macros to rails models and testing it with curl is [here](https://git.generalassemb.ly/ga-wdi-boston/rails-api-many-to-many/blob/master/docs/library_macros.md)

### How to update your database in rails

1. Plan your changes! Update your ERD
2. Create the migration (`bin/rails generate migration` OR create a scaffold or model using a generator which also gives you a migration
- *ALWAYS* **use a migration to make a change to your database.**
- *DO NOT* **edit the schema.rb by hand, or edit the database structure by hand using something like `psql`**. This will break the rails mechanisms which track database changes and allow you to manage your database schema.
3. Run the migration: `bin/rails db:migrate`
4. Double check it worked
- in `bin/rails console`
- in `schema.rb`
- in `psql`
- using curl scripts


### How to add data to your database in rails
1. using the rails console (manually add records and assocate records)
2. using seed files (bulk load example data, seed data, etc)
- example writing seed data loading scripts and rails tasks to run these scripts: [commit](https://git.generalassemb.ly/danman01/rails-api-campus-server/commit/abe370064c6bd37650da5ce1d3d573208b7025c6)

3. using curl scripts
3. using your frontend application

#### Model macros
- ```
@@ -293,7 +314,7 @@ from /Users/dkirschner/.rvm/gems/ruby-2.5.0/gems/activemodel-5.1.4/lib/active_mo
```

### Testing Doctors, Patients and appointments
### Console logs Testing Doctors, Patients and appointments

```
pry(main)> Patient
@@ -456,4 +477,209 @@ pry(main)> Patient
updated_at: Tue, 17 Jul 2018 18:54:02 UTC +00:00,
doctor_id: nil>]
```
```

### Console log from Recipe, Ingredient, Meals many-to-many lab

```
pry(main)> Ingredient
=> Ingredient(id: integer, name: string, unit: string, created_at: datetime, updated_at: datetime)
[2] pry(main)> Recipe
# No model / class exists!
NameError: uninitialized constant Recipe
from (pry):2:in `<main>'
[3] pry(main)> exit
dkirschner@63.95.30.172.o.wireless.uri.edu: rails-api-campus-server (tutorial-danny-tuesday)* $ rails c
Running via Spring preloader in process 16232
Loading development environment (Rails 5.1.4)
[1] pry(main)> Recipe
# migration hasn't been run!
=> Recipe(Table doesn't exist)
[2] pry(main)> Ingredient.create(name: 'sugar', unit: 'tbsp')
(0.2ms) BEGIN
SQL (17.2ms) INSERT INTO "ingredients" ("name", "unit", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id" [["name", "sugar"], ["unit", "tbsp"], ["created_at", "2018-07-18 14:45:11.957020"], ["updated_at", "2018-07-18 14:45:11.957020"]]
(14.5ms) COMMIT
=> #<Ingredient:0x00007f8d4e899238
id: 1,
name: "sugar",
unit: "tbsp",
created_at: Wed, 18 Jul 2018 14:45:11 UTC +00:00,
updated_at: Wed, 18 Jul 2018 14:45:11 UTC +00:00>
[3] pry(main)> Recipe
=> Recipe(Table doesn't exist)
[4] pry(main)> reload!
Reloading...
=> true
[5] pry(main)> Recipe
=> Recipe(id: integer, name: string, description: string, created_at: datetime, updated_at: datetime)
[6] pry(main)> Ingredient
=> Ingredient(id: integer, name: string, unit: string, created_at: datetime, updated_at: datetime)
[7] pry(main)> RecipeIngredient
NameError: uninitialized constant RecipeIngredient
from (pry):7:in `<main>'
[8] pry(main)> Meal
NameError: uninitialized constant Meal
from (pry):8:in `<main>'
[9] pry(main)> reload!
Reloading...
=> true
[10] pry(main)> Meal
=> Meal(id: integer, ingredient_id: integer, recipe_id: integer, created_at: datetime, updated_at: datetime)
[11] pry(main)> Ingredient.last
Ingredient Load (0.7ms) SELECT "ingredients".* FROM "ingredients" ORDER BY "ingredients"."id" DESC LIMIT $1 [["LIMIT", 1]]
=> #<Ingredient:0x00007f8d4af8dbd8
id: 1,
name: "sugar",
unit: "tbsp",
created_at: Wed, 18 Jul 2018 14:45:11 UTC +00:00,
updated_at: Wed, 18 Jul 2018 14:45:11 UTC +00:00>
[12] pry(main)> Ingredient.last.recipes
Ingredient Load (0.5ms) SELECT "ingredients".* FROM "ingredients" ORDER BY "ingredients"."id" DESC LIMIT $1 [["LIMIT", 1]]
NoMethodError: undefined method `recipes' for #<Ingredient:0x00007f8d4c1aa578>
from /Users/dkirschner/.rvm/gems/ruby-2.5.0/gems/activemodel-5.1.4/lib/active_model/attribute_methods.rb:432:in `method_missing'
[13] pry(main)> reload!
Reloading...
=> true
[14] pry(main)> Meal
=> Meal(id: integer, ingredient_id: integer, recipe_id: integer, created_at: datetime, updated_at: datetime)
[15] pry(main)> recipe = Recipe.create(name: 'Waffles', description: 'Breakfast food')
(0.2ms) BEGIN
(6.3ms) ROLLBACK
=> #<Recipe:0x00007f8d4be884a8
id: nil,
name: "Waffles",
description: "Breakfast food",
created_at: nil,
updated_at: nil>
[16] pry(main)> recipe.errors
=> #<ActiveModel::Errors:0x00007f8d50090cb0
@base=
#<Recipe:0x00007f8d4be884a8
id: nil,
name: "Waffles",
description: "Breakfast food",
created_at: nil,
updated_at: nil>,
@details={:user=>[{:error=>:blank}]},
@messages={:user=>["must exist"]}>
[17] pry(main)> reload!
Reloading...
=> true
[18] pry(main)> recipe = Recipe.create(name: 'Waffles', description: 'Breakfast food')
(0.2ms) BEGIN
SQL (14.2ms) INSERT INTO "recipes" ("name", "description", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id" [["name", "Waffles"], ["description", "Breakfast food"], ["created_at", "2018-07-18 15:11:32.947846"], ["updated_at", "2018-07-18 15:11:32.947846"]]
(5.4ms) COMMIT
=> #<Recipe:0x00007f8d4ac465b0
id: 1,
name: "Waffles",
description: "Breakfast food",
created_at: Wed, 18 Jul 2018 15:11:32 UTC +00:00,
updated_at: Wed, 18 Jul 2018 15:11:32 UTC +00:00>
[19] pry(main)> recipe.persisted?
=> true
[20] pry(main)> ingredient = Ingredient.create()
[20] pry(main)> Ingredient
=> Ingredient(id: integer, name: string, unit: string, created_at: datetime, updated_at: datetime)
[21] pry(main)> ingredient = Ingredient.create(name: 'flour', unit: 'cup')
(0.3ms) BEGIN
SQL (0.5ms) INSERT INTO "ingredients" ("name", "unit", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id" [["name", "flour"], ["unit", "cup"], ["created_at", "2018-07-18 15:13:31.758405"], ["updated_at", "2018-07-18 15:13:31.758405"]]
(9.0ms) COMMIT
=> #<Ingredient:0x00007f8d4e829780
id: 2,
name: "flour",
unit: "cup",
created_at: Wed, 18 Jul 2018 15:13:31 UTC +00:00,
updated_at: Wed, 18 Jul 2018 15:13:31 UTC +00:00>
[22] pry(main)> meal = Meal.new(ingredient.id, recipe.id)
ArgumentError: wrong number of arguments (given 2, expected 0..1)
from /Users/dkirschner/.rvm/gems/ruby-2.5.0/gems/activerecord-5.1.4/lib/active_record/core.rb:330:in `initialize'
[23] pry(main)> meal = Meal.new({ingredient_id: ingredient.id, recipe_id: recipe.id})
=> #<Meal:0x00007f8d4be25c90 id: nil, ingredient_id: 2, recipe_id: 1, created_at: nil, updated_at: nil>
[24] pry(main)> hash = { key: 'value' }
=> {:key=>"value"}
[25] pry(main)> hash[:key]
=> "value"
[26] pry(main)> meal.persisted?
=> false
[27] pry(main)> meal.save
(0.3ms) BEGIN
Ingredient Load (1.1ms) SELECT "ingredients".* FROM "ingredients" WHERE "ingredients"."id" = $1 LIMIT $2 [["id", 2], ["LIMIT", 1]]
Recipe Load (0.4ms) SELECT "recipes".* FROM "recipes" WHERE "recipes"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
SQL (18.6ms) INSERT INTO "meals" ("ingredient_id", "recipe_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id" [["ingredient_id", 2], ["recipe_id", 1], ["created_at", "2018-07-18 15:17:18.807182"], ["updated_at", "2018-07-18 15:17:18.807182"]]
(0.5ms) COMMIT
=> true
[28] pry(main)> meal.recipe
=> #<Recipe:0x00007f8d4ec82ae8
id: 1,
name: "Waffles",
description: "Breakfast food",
created_at: Wed, 18 Jul 2018 15:11:32 UTC +00:00,
updated_at: Wed, 18 Jul 2018 15:11:32 UTC +00:00>
[29] pry(main)> meal.ingredient
=> #<Ingredient:0x00007f8d4eca2f00
id: 2,
name: "flour",
unit: "cup",
created_at: Wed, 18 Jul 2018 15:13:31 UTC +00:00,
updated_at: Wed, 18 Jul 2018 15:13:31 UTC +00:00>
[30] pry(main)> recipe
=> #<Recipe:0x00007f8d4ac465b0
id: 1,
name: "Waffles",
description: "Breakfast food",
created_at: Wed, 18 Jul 2018 15:11:32 UTC +00:00,
updated_at: Wed, 18 Jul 2018 15:11:32 UTC +00:00>
[31] pry(main)> recipe.ingredients
Ingredient Load (0.8ms) SELECT "ingredients".* FROM "ingredients" INNER JOIN "meals" ON "ingredients"."id" = "meals"."ingredient_id" WHERE "meals"."recipe_id" = $1 [["recipe_id", 1]]
=> [#<Ingredient:0x00007f8d4e937280
id: 2,
name: "flour",
unit: "cup",
created_at: Wed, 18 Jul 2018 15:13:31 UTC +00:00,
updated_at: Wed, 18 Jul 2018 15:13:31 UTC +00:00>]
[32] pry(main)> ingredient
=> #<Ingredient:0x00007f8d4e829780
id: 2,
name: "flour",
unit: "cup",
created_at: Wed, 18 Jul 2018 15:13:31 UTC +00:00,
updated_at: Wed, 18 Jul 2018 15:13:31 UTC +00:00>
[33] pry(main)> ingredient.recipes
NoMethodError: undefined method `recipes' for #<Ingredient:0x00007f8d4e829780>
from /Users/dkirschner/.rvm/gems/ruby-2.5.0/gems/activemodel-5.1.4/lib/active_model/attribute_methods.rb:432:in `method_missing'
[34] pry(main)> ingredient.cook
NoMethodError: undefined method `cook' for #<Ingredient:0x00007f8d4e829780>
from /Users/dkirschner/.rvm/gems/ruby-2.5.0/gems/activemodel-5.1.4/lib/active_model/attribute_methods.rb:432:in `method_missing'
[35] pry(main)> reload!
Reloading...
=> true
[36] pry(main)> ingredient = Ingredient.last
Ingredient Load (0.6ms) SELECT "ingredients".* FROM "ingredients" ORDER BY "ingredients"."id" DESC LIMIT $1 [["LIMIT", 1]]
=> #<Ingredient:0x00007f8d4af3be00
id: 2,
name: "flour",
unit: "cup",
created_at: Wed, 18 Jul 2018 15:13:31 UTC +00:00,
updated_at: Wed, 18 Jul 2018 15:13:31 UTC +00:00>
[37] pry(main)> ingredient.recipes
Recipe Load (0.6ms) SELECT "recipes".* FROM "recipes" INNER JOIN "meals" ON "recipes"."id" = "meals"."recipe_id" WHERE "meals"."ingredient_id" = $1 [["ingredient_id", 2]]
=> [#<Recipe:0x00007f8d4acef7a0
id: 1,
name: "Waffles",
description: "Breakfast food",
created_at: Wed, 18 Jul 2018 15:11:32 UTC +00:00,
updated_at: Wed, 18 Jul 2018 15:11:32 UTC +00:00>]
# using an instance method `cook` defined in the Ingredient class (ingredient.rb)
[38] pry(main)> ingredient.cook
cooking
=> nil
[39] pry(main)> recipe = Recipe.last
Recipe Load (0.4ms) SELECT "recipes".* FROM "recipes" ORDER BY "recipes"."id" DESC LIMIT $1 [["LIMIT", 1]]
=> #<Recipe:0x00007f8d4ea6d9b0
id: 1,
name: "Waffles",
description: "Breakfast food",
created_at: Wed, 18 Jul 2018 15:11:32 UTC +00:00,
updated_at: Wed, 18 Jul 2018 15:11:32 UTC +00:00>
```

0 comments on commit 621b410

Please sign in to comment.