# Error in has\_many :through

**URL:** <https://www.ruby-forum.com/t/error-in-has-many-through/84137>\
**Category:** Rails\
**Created:** [February 1, 2007, 7:41pm UTC](https://www.ruby-forum.com/t/error-in-has-many-through/84137 "2007-02-01T19:41:50Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![anildigital](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a5b964/32.png) [@anildigital](https://www.ruby-forum.com/u/anildigital)\
**Post date:** [February 1, 2007, 7:41pm UTC](https://www.ruby-forum.com/t/error-in-has-many-through/84137/1 "2007-02-01T19:41:50Z")

</div>

When I was using habtm

product.categories.replace(…category objects) was working fine

But when I started using has\_many :through

other relations are working, but I am not able to do

product.categories.replace(…category objects)

What is the solution then?

Regards,  
Anil W.

---

<div class="post-metadata">

**Author:** ![anildigital](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a5b964/32.png) [@anildigital](https://www.ruby-forum.com/u/anildigital)\
**Post date:** [February 1, 2007, 9:34pm UTC](https://www.ruby-forum.com/t/error-in-has-many-through/84137/2 "2007-02-01T21:34:04Z")

</div>

> What is the solution then?  
> Can you post your relationship declarations for the product and  
> category models?

Steve

---

<div class="post-metadata">

**Author:** ![anildigital](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a5b964/32.png) [@anildigital](https://www.ruby-forum.com/u/anildigital)\
**Post date:** [February 2, 2007, 5:15am UTC](https://www.ruby-forum.com/t/error-in-has-many-through/84137/3 "2007-02-02T05:15:50Z")

</div>

Hi Steve,

These are relationship declarations for product, category and  
CategoriesProduct models!

class Product \< ActiveRecord::Base

has\_many :categories\_products  
has\_many :categories, :through =\> :categories\_products

class Category \< ActiveRecord::Base

has\_many :categories\_products  
has\_many :products, :through =\> :categories\_products

class CategoriesProduct \< ActiveRecord::Base  
belongs\_to :category  
belongs\_to :product  
end

On 2/2/07, Steve B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > What is the solution then?  
> > Can you post your relationship declarations for the product and  
> > category models?
> 
> Steve

–  
Don’t live to geek; geek to live.  
[http://anildigital.blogspot.com](http://anildigital.blogspot.com)

---

<div class="post-metadata">

**Author:** ![anildigital](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a5b964/32.png) [@anildigital](https://www.ruby-forum.com/u/anildigital)\
**Post date:** [February 4, 2007, 6:18pm UTC](https://www.ruby-forum.com/t/error-in-has-many-through/84137/4 "2007-02-04T18:18:57Z")

</div>

Thanks Josh,

I did the same.

Regards,  
Anil W.

On 2/4/07, Josh S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > 
> 
> Josh S.  
> [http://blog.hasmanythrough.com/](http://blog.hasmanythrough.com/)
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> >

–  
Don’t live to geek; geek to live.  
[http://anildigital.blogspot.com](http://anildigital.blogspot.com)

---

<div class="post-metadata">

**Author:** ![anildigital](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a5b964/32.png) [@anildigital](https://www.ruby-forum.com/u/anildigital)\
**Post date:** [February 4, 2007, 5:55pm UTC](https://www.ruby-forum.com/t/error-in-has-many-through/84137/5 "2007-02-04T17:55:42Z")

</div>

Anil W. wrote:

> When I was using habtm
> 
> product.categories.replace(…category objects) was working fine
> 
> But when I started using has\_many :through
> 
> other relations are working, but I am not able to do
> 
> product.categories.replace(…category objects)
> 
> What is the solution then?

#replace is not implemented for has\_many :through associations. You can  
roll your own though. Just delete the join model records of old ones not  
in the new set, then create join model records for the new ones not in  
the old set. And of course you should wrap that in a transaction.

–  
Josh S.  
[http://blog.hasmanythrough.com/](http://blog.hasmanythrough.com/)
