Error in has_many :through

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.

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

Steve

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. [email protected] 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

Thanks Josh,

I did the same.

Regards,
Anil W.

On 2/4/07, Josh S. [email protected] wrote:

Josh S.
http://blog.hasmanythrough.com/


Posted via http://www.ruby-forum.com/.


Don’t live to geek; geek to live.
http://anildigital.blogspot.com

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/