Pretty sure that these tests work correctly. Got them crashing by deleting the dependent :: destroy options in has_many: relationships and has_many: reverse_connections in user.rb.
I wanted to share what I did if someone else works through the Michael Hartl Rails Tutorial 2nd Edition, chapter 11 Exercises.
Several questions arose from this exercise (see bottom of this post). If anyone could help, that would be great.
Chapter 11, Exercise 1:
Add tests for the dependencies: destroy in the relationship model (Listing 11.4 and Listing 11.16), following the example in Listing 10.15.
Here is my test: specs / models / user_spec.rb
require 'spec_helper' describe User do before do @user = User.new(name: "Example User", email: " user@example.com ", password: "foobar", password_confirmation: "foobar") end subject { @user } [...code omitted...] describe "relationship associations" do let(:other_user) { FactoryGirl.create(:user) } before do @user.save @user.follow!(other_user) other_user.follow!(@user) end it "should destroy associated relationships" do relationships = @user.relationships @user.destroy relationships.should be_empty end it "should destroy associated reverse relationships" do reverse_relationships = @user.reverse_relationships @user.destroy reverse_relationships.should be_empty end end
A couple of questions arose from this exercise:
Question 1:
My initial tests were relationships. should be_nil reverse_relationships.should be_nil
But, the implemented array was still returning despite the absence of the user. So, when the user does not exist and the association method is called, the result is an array? It's always like that?
Question 2:
I wanted to play around with deleting relationships and backlinks for the user in the rails console.
I tried this
> user = User.first > user.relationships
How do I actually destroy relationships forever? It seems to be a good thing to know when learning from the console.
Thanks! I'm still pretty new to Rails
Bret sanders
source share