# Deleting join association of has\_many :through

**URL:** <https://www.ruby-forum.com/t/deleting-join-association-of-has-many-through/97843>\
**Category:** Rails\
**Created:** [July 10, 2006, 10:12pm UTC](https://www.ruby-forum.com/t/deleting-join-association-of-has-many-through/97843 "2006-07-10T22:12:45Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [July 10, 2006, 10:12pm UTC](https://www.ruby-forum.com/t/deleting-join-association-of-has-many-through/97843/1 "2006-07-10T22:12:45Z")

</div>

I’m trying to use has\_many :through, since my join model deserves being  
more than just an intersection table.

But when I try to break the association, the break only seems  
“temporary”:

Let’s say my two tables are Users and Colors, and the join model is  
Favorites.

user = Users.find(1)  
user.colors.length

> > 2  
> > c = user.colors.first
> 
> > #Color:....

user.colors.delete©  
user.colors.length

> > 1

Now what do I do here to make this persist?

If I reload my page, user.colors.length == 2 again, and the old color is  
still a part of the collection.

I’ve tried reloading the collection after deleting, hoping that would  
refresh the associations:

user.colors(true)

but I get 2 again, not 1.

Any ideas?

(I know, I know, I’m a moron…)

Thanks  
Jeff

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [July 10, 2006, 10:40pm UTC](https://www.ruby-forum.com/t/deleting-join-association-of-has-many-through/97843/2 "2006-07-10T22:40:17Z")

</div>

Jeff C. wrote on 10.07.2006 22:12:

> I’m trying to use has\_many :through, since my join model deserves being  
> […]
> 
> If I reload my page, user.colors.length == 2 again, and the old color is  
> still a part of the collection.

I think there is a problem in your table relations.

:through: Specifies a Join Model to perform the query through. Options  
for :class\_name and :foreign\_key are ignored, as the association uses  
the source reflection. You can only use a :through query through a  
belongs\_to or has\_many association.

Have you noticed the last sentence?

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [July 10, 2006, 10:46pm UTC](https://www.ruby-forum.com/t/deleting-join-association-of-has-many-through/97843/3 "2006-07-10T22:46:55Z")

</div>

Markus K. wrote:

> :through: Specifies a Join Model to perform the query through. Options  
> for :class\_name and :foreign\_key are ignored, as the association uses  
> the source reflection. You can only use a :through query through a  
> belongs\_to or has\_many association.
> 
> Have you noticed the last sentence?

I think I’m doing it right.

class User  
has\_many :favorites  
has\_many :colors, :through =\> :favorites  
end

class Color  
has\_many :favorites  
has\_many :users, :through =\> :favorites  
end

class Favorite  
belongs\_to :user  
belongs\_to :color  
end

Are you saying I’ve still got the relationships wrong somehow?

def delete\_color  
color = Color.find(params[:color\_id])  
user.colors.delete(color) # this works in script/console as expected

# at this point, user.colors.length is correct

redirect :action =\> :show # this will show 2 favorite colors again:  
user.colors.length is wrong  
end

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [July 11, 2006, 2:29am UTC](https://www.ruby-forum.com/t/deleting-join-association-of-has-many-through/97843/4 "2006-07-11T02:29:03Z")

</div>

Rick O. wrote on 11.07.2006 00:34:

> You’ll want to delete the Favorite object that joins them.
> 
> user.favorites.find\_by\_color\_id(params[:color\_id]).destroy

But it should be possible over colors collection, too.  
Do you need a user.colors.save afterwards?

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [July 11, 2006, 12:39am UTC](https://www.ruby-forum.com/t/deleting-join-association-of-has-many-through/97843/5 "2006-07-11T00:39:03Z")

</div>

> Are you saying I’ve still got the relationships wrong somehow?
> 
> def delete\_color  
> color = Color.find(params[:color\_id])  
> user.colors.delete(color) # this works in script/console as expected
> 
> # at this point, user.colors.length is correct
> 
> redirect :action =\> :show # this will show 2 favorite colors again:  
> user.colors.length is wrong  
> end

You’ll want to delete the Favorite object that joins them.

user.favorites.find\_by\_color\_id(params[:color\_id]).destroy

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [July 11, 2006, 3:33am UTC](https://www.ruby-forum.com/t/deleting-join-association-of-has-many-through/97843/6 "2006-07-11T03:33:35Z")

</div>

Markus K. wrote:

> Rick O. wrote on 11.07.2006 00:34:
> 
> > You’ll want to delete the Favorite object that joins them.
> > 
> > user.favorites.find\_by\_color\_id(params[:color\_id]).destroy
> 
> But it should be possible over colors collection, too.  
> Do you need a user.colors.save afterwards?

user.colors.save isn’t possible (method missing), but that was a good  
clue.

user.save! does the trick.

Thanks Markus!

Jeff

> **[Hey Daddio!](http://softiesonrails.com)**
>
> Home of Hey Daddio Software

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [July 11, 2006, 2:47pm UTC](https://www.ruby-forum.com/t/deleting-join-association-of-has-many-through/97843/7 "2006-07-11T14:47:50Z")

</div>

Jeff C. wrote on 11.07.2006 03:33:

> clue.
> 
> user.save! does the trick.

Can you have a look in the log?  
I’m interested in what SQL commands are executed on this save if only  
colors association changed and no other user data.  
Does it only update the association to colors or everything from user  
model?

Thanks  
Markus
