Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add tests for dependent :destroy in the Relationship model (Chapter 11, Exercise 1 Rails Tutorial, 2nd Ed)

Pretty sure these tests are working correctly. Got them to fail by removing the dependent: :destroy options on the has_many :relationships and has_many :reverse_relationships in user.rb.

Wanted to share what I did in case anyone else is working through Michael Hartl's Rails Tutorial 2nd Edition, Chapter 11 Exercises.

A few questions arose from this exercise (see bottom of this post). If anyone could help, that'd be great.

Chapter 11, Exercise 1:

Add tests for dependent :destroy in the Relationship model (Listing 11.4 and Listing 11.16) by following the example in Listing 10.15.

Here's my test: spec/models/user_spec.rb

require 'spec_helper'

describe User do

  before do
  @user = User.new(name: "Example User", email: "[email protected]", 
                   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 questions arose from this exercise:

Question 1:

My initial tests were relationships.should be_nil reverse_relationships.should be_nil

But, realized an array was still being returned, despite no user existing. So, when a user doesn't exist and an association method is called, the result is still an array? Is this always true?

Question 2:

I wanted to play around with deleting relationships and reverse_relationships for a user in the rails console.

I tried this

> user = User.first
> user.relationships
 # returns a bunch of relationships
> user.relationships.destroy
=> []
> user.relationships
 # returns same bunch of relationships

How do I actually destroy the relationships permanently? Seems like good thing to know when exploring in console.

Thanks! I'm still pretty new to Rails

like image 218
Brett Sanders Avatar asked Apr 20 '12 21:04

Brett Sanders


2 Answers

may be you need smt like this

it { should have_many(:relationships).dependent(:destroy) }
like image 125
xab3r Avatar answered Oct 19 '22 13:10

xab3r


I'm a ruby/rails noob too.

Question 1: Searched rubyonrails.org for has_many and it says

Returns an array of all the associated objects. An empty array is returned if none are found.

On a side note, you can test for both nil and empty:

relationships.present?.should be_false

Question 2: The user.relationships.destroy requires an :id

user.relationships.destroy '1'
like image 3
EricM Avatar answered Oct 19 '22 14:10

EricM