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 6081925
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:13:52+00:00 2026-05-23T11:13:52+00:00

So I have a friendships table which looks like this: create_table friendships, :force =>

  • 0

So I have a friendships table which looks like this:

  create_table "friendships", :force => true do |t|
    t.integer "user_id"
    t.integer "friend_id"
    t.integer "status"
  end

For each friendship created two rows are inserted, with user_id and friend_id reverted. When a user is deleted the friendship should be deleted also. This code removes one of them:

  has_many :friendships, :dependent => :destroy

But that only removes one of the friendships. In my friendship model I have this code:

  belongs_to :user
  belongs_to :friend, :class_name => "User", :foreign_key => "friend_id"

I also have a custom method for removing friendships which creates a transaction that calls destroy on both the associated objects.

My solution would be to override the destroy method in User and loop through all its friendships and call my remove method on those. This works, but is it an elegant solution? I feel like there could be a nice Rails-way to do this.

Thanks.

  • 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-05-23T11:13:53+00:00Added an answer on May 23, 2026 at 11:13 am

    The most elegant solution is to have two has_many relations on the User model, one for ‘friends of this user’ (which you’ve already got) and one for ‘users who call this user a friend’. Then set :dependent => :destroy on both of them:

    def User
        # Friends of this user
        has_many :friendships, :dependent => :destroy
        has_many :friends, :through => :friendships
    
        # Users who call this user a friend
        has_many :friendships_of, :class_name => "Friendship", :foreign_key => "friend_id", :dependent => :destroy
        has_many :friends_of, :through => :friendships_of
    end
    

    This will mean that destroying a User also deletes any friendship records that reference that user as either the user_id or the friend_id.

    Removing both the associated objects when breaking a Friendship record seems like a bad idea though – destroying a friendship doesn’t usually mean that both friends cease to exist!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a friendship model like so: Friendship (user_id, friend_id, status) Where there is
Have a procedure which looks like Procedure TestProc(TVar1, TVar2 : variant); Begin TVar1 :=
The problem is the following, I have a table of friendships in my database,
have written this little class, which generates a UUID every time an object of
Say you have a table schema such as this: friends(friendship_id, uid1, uid2, are_friends) .
I have a friends table: id , fid1 , fid2 which represents a friendship
So I have this working properly: CREATE FUNCTION dbo.GetLiveStream(@UserName NVARCHAR(MAX)) RETURNS TABLE AS RETURN
I have a table with such structure: uid sid all integers. This table simulates
I want to have a table with columns: id, user_id1, and user_id2. basically this
I have a table of member-to-member connections. The schema is member_id, friend_id, is_active. I

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.