Forum: Ruby on Rails error in has_many :through

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Anil W. (Guest)
on 2007-02-01 20:41
(Received via mailing list)
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.
Stephen B. (Guest)
on 2007-02-01 22:34
(Received via mailing list)
> What is the solution then?
Can you post your relationship declarations for the product and
category models?

Steve
Anil W. (Guest)
on 2007-02-02 06:15
(Received via mailing list)
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> 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
Josh S. (Guest)
on 2007-02-04 18:55
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/
Anil W. (Guest)
on 2007-02-04 19:18
(Received via mailing list)
Thanks Josh,

I did the same.

Regards,
Anil W.

On 2/4/07, Josh S. <removed_email_address@domain.invalid> 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
This topic is locked and can not be replied to.