Preventing Dups in HABTM

I have a habtm that looks like:

report
has_and_belongs_to_many :subscribers

subscriber
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
report.subscribers.find_by_id(subscriber_id).nil?

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

Thanks


View this message in context:
http://www.nabble.com/Preventing-Dups-in-HABTM-t1819715.html#a4961706
Sent from the RubyOnRails Users forum at Nabble.com.

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