loading...

Day 51 : #100DaysofCode - Code Along

sincerelybrittany profile image Brittany ・1 min read

My post previous post mentioned that I will add an item to a list.

In order to create a list rails g model item description which generates:

  invoke  active_record
  create    db/migrate/20200719021641_create_items.rb
  create    app/models/item.rb
  invoke    test_unit
  create      test/models/item_test.rb
  create      test/fixtures/items.yml

Then you have to migrate your database and create your schema by running rails db:migrate and then set up your associations in the models. A List has many items and an Item has many lists.

Then as always check your database within your console.

rails c
list = List.create(:description => "cookies")
milk = Item.create
milk.description = "Milk"
milk
milk.list_id = list.id
milk.list_id = list
milk.save
milk.list

Great everything works. The has_many in items creates many methods and allows us to do something like this:

list.items.create(:description => "new shoes")

Feel free to checkout the final code on Github.

As always, thanks for reading!

Sincerely,
Brittany

Posted on by:

sincerelybrittany profile

Brittany

@sincerelybrittany

Developer | Software Engineer πŸ‘©πŸΎβ€πŸ’» | Determined | Music & Dance | Completed #100DaysofCode | #WomenWhoCode

Discussion

markdown guide