Rails 6 API Tutorial - Creating Multiple Records in Controllers p.13

Rails 6 API Tutorial - Creating Multiple Records in Controllers p.13

Rails Pro

3 года назад

4,660 Просмотров

In this video series we will build a Rails API from scratch. Backend APIs are useful for serving data to frontend applications, mobile apps or other backend services.

✨ Check out the new version of this video series, which has been updated for Rails 7 https://youtu.be/FJiIvFoxqsc

📖 You can also purchase the eBook and code for the new Rails 7 tutorial - https://tomkadwill.gumroad.com/l/rails-api-tutorial

This video covers:
0:00 - Introduction
0:32 - Benefits of using test driven development (TDD)
0:59 - Running the failing CREATE action spec
2:06 - Rewriting the params in separate book and author hashes
4:02 - Debugging the test failure using binding.irb
5:26 - Creating multiple records in the controller
6:41 - Merging author_id into the book params to associate the two records together
8:16 - Tidying up the book_params method
8:48 - Adding an assertion to check the Author record was created in the DB

If you’re new to this series, you may want to start with part 1: https://youtu.be/6KqbPJtA5O8

Тэги:

#Rails #Rails_API #Rails_API-only #GoRails #Traversy_Media #Deanin #Rails_6 #Rails_6_API #Rails_TDD #Rails_test_driven_development #Rails_API_TDD #Rails_API_binding.irb #Rails_6_API_TDD #Rails_6_TDD #Rails_6_binding.irb #Rails_6_API_Controller_multiple_records #Rails_6_API_controller_associated_records #Rails_6_API_debugging #Rails_API_debugging #Rails_API_multiple_params #Rails_API_merge_params #Rails_API_test_driven_development #Rails_6_API-only_TDD #Rails_6_API_only_TDD
Ссылки и html тэги не поддерживаются


Комментарии:

Ahmed Mustafa
Ahmed Mustafa - 02.08.2022 18:05

Thanks man, this series helped me a lot ❤

Ответить
Fletcher Guevy
Fletcher Guevy - 24.05.2022 15:16

author create should be its own endpoint

Ответить
Skalippan Balippan
Skalippan Balippan - 11.10.2021 20:51

I learned a lot in this video! thank you <3

Ответить
darkpill
darkpill - 07.09.2021 01:06

This is great! How would you handle the strong_params if you wanted to accept an array of authors for a book?

Ответить
jds
jds - 12.08.2021 22:02

Really helpful series. Only suggestion I have here is to use Author.find_or_create_by(author_params) to avoid duplicate authors.

Ответить
Laguna Zootovary
Laguna Zootovary - 25.03.2021 03:10

Thank you a lot. It`s really helpful.

Ответить
Chris Moore
Chris Moore - 10.01.2021 02:24

It would make a lot more sense to have book have accepts_nested_attributes_for :author, then you can just pass the whole params objected Book.create and the book and the author will be created at the same time. It would also rollback correctly if the author was saved, but the book failed validation, in your current implementation this scenario would leave an orphaned author record.

Ответить
jeevan g
jeevan g - 22.12.2020 10:45

311 subscriber

Ответить