Issue 6: Rename the product self referential has_and_belongs_to_many association (related_products)
Status:  Invalid
Owner: ----
Closed:  Feb 2008
Reported by edmundo...@gmail.com, Feb 10, 2008
Version used: substruct_rel_0-97

The self referential has_and_belongs_to_many relationship of the product
class was given a name that don't permits it to take full advantage of the
automatically added methods to manipulate the collection of associated
objects. You can add but you cannot delete related products (now I see why
the SQL was created)

Changing
has_and_belongs_to_many :related_products
...
to
has_and_belongs_to_many :related
...

let you ged rid of that ugly SQL command:

def remove_return_relation(relative)
  sql  = "DELETE FROM related_products "
  sql << "WHERE product_id = #{relative.id} AND related_id = #{self.id}"
  ActiveRecord::Base.connection.execute(sql)
end

doing something like that:

def remove_return_relation(relative)
  relative.related.delete(self)
end

I don't know if you can maintain the same association name.
Feb 10, 2008
#2 edmundo...@gmail.com
Appears that the callback and its respective related_product_ids, that is marked with
the comment "Related products (associations) freak out if this object isn't saved
before adding relatives." can be changed to assign just by suggestion_names (a better
name) as it doesn't assign by ids and a method with that name is already
automatically created.
Feb 11, 2008
#3 edmundo...@gmail.com
Please close this issue as don't making sense.

I really had funny results testing different times.

1. The name of the relation doesn't make any difference.
2. Don't using SQL at the callback of the return relation creates at best performance
problems or infinite calls.
3. The way related products are beeing assigned doesn't seems to make difference if a
product is new or not, (for this I had problems before, but I can't reproduce them
again, and I didn't discovered why it happened).


Feb 12, 2008
Project Member #4 subim...@gmail.com
(No comment was entered for this change.)
Status: Invalid