Preventing Dups in HABTM

I have a habtm that looks like:

has_and_belongs_to_many :subscribers

has_and_belongs_to_many :reports

When I add a report to a subscriber, I have to run code like:

raise “already there, idiot!” unless

Is there a better way to avoid adding duplicative associations? has_many
:through seems like overkill for this application.


View this message in context:
Sent from the RubyOnRails Users forum at

You could put a unique constrain across the ids
in the DB, then add the association in a begin/
rescue block, thereby squashing the error if it’s
a duplicate.

– Tom M.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs