DEV Community

Brittany
Brittany

Posted on

2 2

Day 51 : #100DaysofCode - Code Along

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
Enter fullscreen mode Exit fullscreen mode

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

AWS GenAI LIVE image

Real challenges. Real solutions. Real talk.

From technical discussions to philosophical debates, AWS and AWS Partners examine the impact and evolution of gen AI.

Learn more

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay