Associations:
- Our To-do list has many todo-list items
- A Todo-list item belongs to a todo-list
- belongs_to
- has_one
- has_many
- has_many :through
- has_one :through
- has_and_belongs_to_many
--
1. Install the shoulda-matchers gem:
This will help me test associations without writing a lo lot of code (since I'm working with test-driven development). Remember to run bundle.
2. I'll add todo-items to our to-do list (I could've done a scaffold, but I'm going to do it manually). I'll also use model instead of scaffold because we're going to do something called "nesting" the todo-items inside of our todo-list and resource declaration.
Generate the todo-item model:
bin/rails generate model todo_item todo_list:references content:string
Run: rake db:migrate
Migrate my test database as well:
rake db:migrate RAILS_ENV=test
> models >todo_item.spec
require 'spec_helper'
describe TodoItem do
it {should belong_to (:todo_list)}
end
>models >todo_list.spec
require 'spec_helper'
describe TodoList do
it {should have_many(:todo_items)}
end
todo_list.rb
has_many :todo_items
Alejo likes this.