- ga-wdi-boston/rails-api-bdd
- ga-wdi-boston/rails-activerecord-one-to-many
- ga-wdi-boston/rails-activerecord-many-to-many
By the end of this lesson, students should be able to:
- Explain the value of iterative development.
- Write concise, associated routes.
- Write model unit specs for associations.
- Write model unit specs for validations.
- Fork and clone this repository.
- Install dependencies with
bundle install
. - Run
rake db:create db:migrate
- Create
config/secrets.yml
and runrake secret
twice to generate tokens.
test:
secret_key_base: XXX
secret_token: XXX
Let's check out how our requests/articles_spec.rb has changed since our last pass at this.
Let's look into requests/article_comments_spec.rb and examine what is required to make a request on associations. What do you notice about our endpoints?
Start with modifying our Comments migration
(rails g migration AddArticleToComments
).
Then, we will update our Comments and Articles models to handle this new relationship.
Check out our nested routes in routes.db.
Before we associated Comments to Articles, our Articles routes looked like this:
resources :articles, except: [:new, :edit]
Now that we're associating Comments to Articles, we can take advantage of Rails' nested routes feature. Hence, our routes for these resources now look like this:
resources :articles, except: [:new, :edit] do
resources :comments, only: [:index, :create]
end
resources :comments, except: [:new, :edit]
Comments' :index
and :create
methods are collection routes in Rails,
meaning their actions act upon the collection of their resource, hence why they
are nested (i.e., showing ALL comments associated to an article).
Comments' :update
, :show
, and :destroy
methods are member routes in
Rails, meaning their actions act upon a single member of the resource
collection (i.e., deleting ONE comment).
Let's run rake routes
and take a look at what this gives us.
Nested routes offer many advantages, but can still look a little sloppy depending on the restrictions you need to apply to each resource, respectively.
Rails to the rescue!
We can leverage shallow routes to generate the same routes.
resources :articles, except: [:new, :edit] do
resources :comments, except: [:new, :edit], shallow: true
end
Here, adding shallow: true
to our comments resources generates all collection
routes for the child route association (i.e., :index
and :create
) as well
as all other member routes that are not nested (i.e., :show
, :update
,
:destroy
).
It's your choice whether you use nested or shallow routes. Testing for Article/Comment associated routes can be found at routing/article_comments_spec.rb.
In spec/models/article_spec.rb, let's test to see if we:
- are associating comments to articles
- have set our
inverse_of
record - are deleting comments associated to articles when articles are deleted
In spec/models/comment_spec.rb, use the tests we created for the Article model to guide your tests to ensure you:
- are associating articles to comments
- have set your
inverse_of
record
Using our BDD skills, let's create tests to check that our Article model is
validating the presence of content
and title
. We don't want articles
created that omit either.
We will create our tests first and let those drive us towards an adequately-validated model.
Your turn. Let your test(s) drive you towards validating the presence of a new
comment's content
.
Source code distributed under the MIT license. Text and other assets copyright General Assembly, Inc., all rights reserved.