Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions app/models/item.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class Item < ApplicationRecord
# model association
belongs_to :todo

# validation
validates_presence_of :name
end
7 changes: 7 additions & 0 deletions app/models/todo.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class Todo < ApplicationRecord
# model association
has_many :items, dependent: :destroy

# validations
validates_presence_of :title, :created_by
end
10 changes: 10 additions & 0 deletions db/migrate/20211110194428_create_todos.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class CreateTodos < ActiveRecord::Migration[6.1]
def change
create_table :todos do |t|
t.string :title
t.string :created_by

t.timestamps
end
end
end
11 changes: 11 additions & 0 deletions db/migrate/20211110194855_create_items.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class CreateItems < ActiveRecord::Migration[6.1]
def change
create_table :items do |t|
t.string :name
t.boolean :done
t.references :todo, null: false, foreign_key: true

t.timestamps
end
end
end
32 changes: 32 additions & 0 deletions db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions spec/models/item_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
require 'rails_helper'

RSpec.describe Item, type: :model do
# Association test
# ensure an item record belongs to a single todo record
it { should belong_to(:todo) }
# Validation test
# ensure column name is present before saving
it { should validate_presence_of(:name) }
end
11 changes: 11 additions & 0 deletions spec/models/todo_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require 'rails_helper'

RSpec.describe Todo, type: :model do
# Association test
# ensure Todo model has a 1:m relationship with the Item model
it { should have_many(:items).dependent(:destroy) }
# Validation tests
# ensure columns title and created_by are present before saving
it { should validate_presence_of(:title) }
it { should validate_presence_of(:created_by) }
end