A project has many tasks - has_many , each task belongs to a belongs_to project .
Model generation
rails g model Project title:string # project:references - создает отношение таблиц в терминах миграции rails g model Todo text:string isCompleted:boolean project:references
Or immediately scaffold (to create controllers, models, etc.)
rails g scaffold Project title:string rails g scaffold Todo text:string isCompleted:boolean project:references
After creating and before the migration, you can add the necessary parameters to the migration, for example, the default values for the fields.
After complete the migration (update database schema)
rails db:migrate
In model classes (app / models / *)
class Project < ApplicationRecord has_many :todos end class Todo < ApplicationRecord belongs_to :project end
Can now use
@project = Project.create({title: 'Project 1'}) @project.todos.create({text: 'todo 1', isCompleted: false}) @todos = @project.todos.all
rails g model Название поле:тип поле2:тип. - D-side