Ask A Question


You’re not receiving notifications from this thread.

Question regarding creating new items with belongs_to association!!

MasonLogan asked in General

Hi, I am completely new to Rails and would like some help. I am trying to implement a to-do list using Rails as my backend and React as my frontend. Initially, I created a model and controller that would allow me to create/update/delete tasks. Now I would like to classify them into their categories. The first thing I did was to create a another model Category that has_many tasks and then the Task model belongs_to category.

Now, previously everything was submitted as JSON to the Tasks controller which then created the task. However, now that I've created the relations between Task and Category, how do I go about submitting my form data? The form data will contain only description (task description) and category. Should I send the data to Categories controller? If yes, then how do I let the controller know to create a new category in the categories database if the category doesn't exist. After which, how do I create the task in the tasks database and assign it the category_id?

I've tried searching online for help but everything was about creating the relations which I've already done. But I just have no idea how to create the new tasks in the controllers.

Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 83,038+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.