The primary difference between has_and_belongs_to_many (HABTM) and has_many :through (HMT) is that HMT introduces a new Model, whereas HABTM does not.
With HABTM, all you can do is
find all users who are fans of a band
find all bands who a user is a fan of.
With HMT, you introduce a new model , let's call it 'Fanship'
As you introduce attributes into the Fanship model, you can do things like
Find all bands who have fans because of 'interest_tag'
Find all Bands who have fans because of 'song_tag'
So you may end up with queries like
@song_fans = Fanship.find(:all, :conditions=>['band_id = ? AND song_id = ?', @band.id,@song.id])
@type_fans = Fanship.find(:all, :conditions=>['band_id = ? AND interest_tag = ?', @band.id, @interest.id])
And your Fanship model would be defined like
class AddFanship< ActiveRecord::Migration
create_table :fanship do |t|
Last edited by BradHodges (2011-10-31 00:43:41)
Joe got a job, on the day shift, at the Utility Muffin Research Kitchen, arrogantly twisting the sterile canvas snout of a fully charged icing anointment utensil.