# Remove from many-to-many relationship

**URL:** <https://www.ruby-forum.com/t/remove-from-many-to-many-relationship/88348>\
**Category:** Rails\
**Created:** [March 9, 2007, 10:09pm UTC](https://www.ruby-forum.com/t/remove-from-many-to-many-relationship/88348 "2007-03-09T22:09:15Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![davosian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f05b48/32.png) [@davosian](https://www.ruby-forum.com/u/davosian)\
**Post date:** [March 9, 2007, 10:09pm UTC](https://www.ruby-forum.com/t/remove-from-many-to-many-relationship/88348/1 "2007-03-09T22:09:15Z")

</div>

I have a has\_and\_belongs\_to\_many relationship between Tournament and  
Team.

How can I remove a team from a tournament without deleting the team  
itself, e.g something along these lines.:

tou = Tournament.find(:first)  
tou.remove\_team(:id =\> 1)

Thanks,  
Dennis

---

<div class="post-metadata">

**Author:** ![davosian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f05b48/32.png) [@davosian](https://www.ruby-forum.com/u/davosian)\
**Post date:** [March 9, 2007, 11:50pm UTC](https://www.ruby-forum.com/t/remove-from-many-to-many-relationship/88348/2 "2007-03-09T23:50:50Z")

</div>

davosian wrote:

> I have a has\_and\_belongs\_to\_many relationship between Tournament and  
> Team.
> 
> How can I remove a team from a tournament without deleting the team  
> itself, e.g something along these lines.:
> 
> tou = Tournament.find(:first)  
> tou.remove\_team(:id =\> 1)
> 
> Thanks,  
> Dennis

Tournament.find(:first).teams.delete\_if {|team| team.id == 1}

sprinkle error checking to taste…

ilan

---

<div class="post-metadata">

**Author:** ![davosian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f05b48/32.png) [@davosian](https://www.ruby-forum.com/u/davosian)\
**Post date:** [March 10, 2007, 4:00am UTC](https://www.ruby-forum.com/t/remove-from-many-to-many-relationship/88348/3 "2007-03-10T04:00:48Z")

</div>

> > t = Team.new :name =\> “olympic”  
> > =\> #\<Team:0x31ea6c4 @new\_record=true, @attributes={“name”=\>“olympic”}  
> > t.save  
> > =\> true  
> > tour = Tournament.new :name =\> “World Cup”  
> > =\> #\<Tournament:0x31d28bc @new\_record=true,  
> > @attributes={“name”=\>“World Cup”}  
> > tour.save  
> > =\> true  
> > t.tournaments  
> > =\> [#\<Tournament:0x31ca1d0 @attributes={“team\_id”=\>“1”, “name”=\>“World  
> > Cup”, “id”=\>“1”, “tournament\_id”=\>“1”}, readonlytrue]  
> > t.tournaments.size  
> > =\> 1  
> > t.tournaments.delete(tour)  
> > =\> [#\<Tournament:0x31d28bc @new\_record\_before\_save=true,  
> > @errors=#\<ActiveRecord::Errors:0x31ce9c4 @errors={},  
> > @base=#\<Tournament:0x31d28bc …\>, new\_recordfalse,  
> > attributes{“name”=\>“World Cup”, “id”=\>1}]

This will delete the record in the association table that connects the  
team to the tournament.

class CreateTeams \< ActiveRecord::Migration  
def self.up  
create\_table :teams do |t|  
t.column :name, :string  
end

```
create_table :teams_tournaments do |t|
  t.column :team_id, :integer
  t.column :tournament_id, :integer
end

```

end

def self.down  
drop\_table :teams  
end  
end

class Tournament \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :teams  
end

class Team \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :tournaments  
end

class CreateTournaments \< ActiveRecord::Migration  
def self.up  
create\_table :tournaments do |t|  
t.column :name, :string  
end  
end

def self.down  
drop\_table :tournaments  
end  
end

I manually created the record in the join table to create the  
relationship between the team and the tournament before I fired up the  
script/console session.

So, you have to pass the tournament object not the id when you delete.  
For more on this check out :

> **[ActiveRecord::Associations::ClassMethods](https://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#M000645)**
>
> Associations are a set of macro-like class methods for tying objects together through foreign keys.
