thanks for all the replies. just to clarify my intent, i am associating keywords with a set of documents, and the more times a keyword is associated with particular document, the more it is weighted as being relevant to that keyword, but it must be the exact same keyword, that's why i do this:
a.bs << b a.bs << b a.bs << b so I was NOT asking for this: a.bs << b1 a.bs << b2 a.bs << b3 and when an association disappears, i want to remove one of the associations and one only, b/c the cardinality of duplicate associations is important for my app. I explored the idea of an increment counter, but then I have to add extra logic to the controller or model. So ultimately, I refactored to has_many :through, and now I can just remove one of the has_many's. I realize now, too, that this makes more sense philisophically, as one of the intents of has_many :through is to give the many an equal state as a bona fide model, whereas the habtm is a cardinality-less association by design (that's my conclusion any way). thanks for all your help! dino On Sep 17, 4:09 pm, Marnen Laibow-Koser <rails-mailing-l...@andreas- s.net> wrote: > Robert Walker wrote: > > [...] > > >> a.bs << b > >> a.bs << b > >> a.bs << b > > > This is somewhat unclear, but it appears from your code this is adding > > the same b three times. So yes, if you did exactly as shown all three > > association to that same b would be deleted. > > This appears to be what Dino was asking. > > > > > a.bs << b1 > > a.bs << b2 > > a.bs << b3 > > > a.bs.delete(b2) would properly delete only b2 and leaving b1 and b3. > > This is expected behavior. The only thing I don't like about how Rails > > handles this is that << will create copies of the same associations. So > > I typically add a unique index across the two primary keys of the join > > table to prevent duplicating associations. > > Yeah, that makes a lot of sense. I tended to do that on join tables > before I ever heard of Rails. > > > That being said, I also use > > has_many :through in all cases (as Greg mentioned). I basically pretend > > HABTM does not exist. > > This, on the other hand, makes no sense at all to me. For a simple > habtm, nothing at all is gained by using has_many :through instead. If > you outgrow the simple habtm, refactoring to has_many at that time is > quick and easy. I agree that habtm is very limited, but it's a nice > shortcut where it works. There's no reason to avoid it entirely. > > Best, > -- > Marnen Laibow-Koserhttp://www.marnen.org > mar...@marnen.org > > -- > Posted viahttp://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk@googlegroups.com To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---