I have one more quick question. Now that I'm using a habtm relationship, is there a nice way to remove the inverse relationship on delete. Say I have a Product (p1) and it has an associated product (p2), when p2 is deleted I'd like the relationship to also be removed - so from a DB standpoint I want to do something like "DELETE FROM associated_products WHERE product_id=id OR associated_id=id" instead of the standard cascade delete on product_id.
Thanks On Apr 8, 9:36 am, anton <[email protected]> wrote: > Great, that's working fine now. Many thanks Sergio. > > On Apr 8, 9:24 am, "Sergio Cambra .:: entreCables S.L. ::." > > <[email protected]> wrote: > > On Miércoles, 7 de Abril de 2010 16:59:26 anton escribió: > > > > Hi, > > > > Hoping someone can help me with this. I have a :has_many :through self > > > referential association. Creating/Adding/Removing Products and nested > > > associated products in working fine apart from when you try to remove > > > the last existing Product of the association. The update isn't > > > triggered on the association table (I'm presuming because there is no > > > collection in the updated model to trigger the associated update/ > > > delete). > > > > See example below, where a Product can have many AssociatedProducts: > > > > Tables: > > > create_table :products do |t| > > > t.string :title, :null => false > > > end > > > > create_table :associated_products do |t| > > > t.references :product, :null => false > > > t.integer :associated_id, :null => false > > > end > > > > Models: > > > class Product < ActiveRecord::Base > > > has_many :associated_products, :dependent => :destroy > > > has_many :associates, :through => :associated_products > > > end > > > > class AssociatedProduct < ActiveRecord::Base > > > belongs_to :product > > > belongs_to :associate, :class_name => "Product", :foreign_key > > > => :associated_id > > > end > > > It's a bug in has_many through support. Really you don't need has_many > > through > > in this case, you can do the same with habtm (which is working): > > class Product < ActiveRecord::Base > > has_and_belongs_to_many :associates, :class_name => 'Product', :join_table > > => :associated_products, :association_foreign_key => :associate_id > > end > > > Anyway, I have fixed removing last associated record from has_many through > > > > Controllers: > > > class ProductsController < ApplicationController > > > active_scaffold :product do |config| > > > config.columns = [ :title, :associates ] > > > config.columns[:associates].form_ui = :select > > > # Same behaviour whether draggable_lists or standard checkboxes > > > are used > > > #config.columns[:associates].options = {:draggable_lists => true} > > > config.columns[:associates].label = "Associated Products" > > > end > > > end > > > > Any help on this greatly appreciated. > > > > Cheers, > > > > Anton > > > -- > > Sergio Cambra .:: entreCables S.L. ::. > > Mariana Pineda 23, 50.018 Zaragoza > > T) 902 021 404 F) 976 52 98 07 E) [email protected] -- You received this message because you are subscribed to the Google Groups "ActiveScaffold : Ruby on Rails plugin" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/activescaffold?hl=en.
