[Ruby] has_and_belongs_to_many to has_many through

Aaron Patterson aaron.patterson at gmail.com
Tue Feb 27 10:43:42 PST 2007


I'm converting some habtm relationships to has_many though relationships and
I want to maintain the same api.  So far when I append to my has_many
relationship, I get a new record in my join table.  But for some reason when
I clear the array, none of the records in the join table get removed!

I *can* clear the array on the join table, but I don't want to do that since
that means changing the api.

Does anyone have any insight?

Here's a shortened example:

class Metro < ActiveRecord::Base
  has_many :audiences_metros, :class_name => 'AudiencesMetros'
  has_many :audiences, :through => :audiences_metros
end

class Audience < ActiveRecord::Base
  has_many :audiences_metros, :class_name => 'AudiencesMetros'
  has_many :metros, :through => :audiences_metros
end

class AudiencesMetros < ActiveRecord::Base
  belongs_to :metro
  belongs_to :audience
end

This works:

  audience.audiences_metros.clear

This doesn't:

  audience.metros.clear

Thanks!

-- 
Aaron Patterson
http://tenderlovemaking.com/


More information about the Ruby mailing list