Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7842731
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T16:23:25+00:00 2026-06-02T16:23:25+00:00

Pretty sure these tests are working correctly. Got them to fail by removing the

  • 0

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: "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 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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T16:23:26+00:00Added an answer on June 2, 2026 at 4:23 pm

    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'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Pretty sure there is an easy answer to this, but just can't find the
I'm pretty sure there is no way, but i'm putting this out there for
I'm pretty sure there is an easier way to do this that I'm missing
I'm pretty sure there is a very simple solution here but I can't seem
In the Java code for our application I'm pretty sure there are lots of
I will be releasing my Air/Flex application soon, but I am pretty sure there
I'm having trouble getting phong shading to look right. I'm pretty sure there's something
Pretty sure some people encountered this problem before. Followed all the instructions to setup
Im pretty sure this has a simple solution. I am using jCarousellite, and i
Am pretty sure this is a cake 1.3 question - the plugin is not

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.