Written by Sean Behan on Sun Jun 17th 2012

How to validate uniqueness in a :has_many :through relationship with Ruby on Rails. You'll need three models in this example List, Subscriber and ListSubscriber. A list has many subscribers and a subscriber has many lists. Simple enough. Running the following code would create a new record in our database table "list_subscribers". It will also create two records that are exactly the same.

@subscriber.lists << @list
@list.subscribers << @subscriber

We need to correct this so that a subscriber can't be put on a list twice. If we're emailing our list, this subscriber would get two copies and we need to avoid this for obvious reasons. We need to add a validates_uniqueness_of condition in the model that represents the join table in the many to many relationship, ListSubscriber. ListSubscriber will take advantage of the :scope declaration and assign the :list_id as the qualifier, and throw an exception if we attempt to add a subscriber twice. Below is all the code you need to validate that there are no duplicates. Keep in mind that this is still application logic and therefore won't add constraints to your database. It's possible you'll have duplicates in race conditions etc. This is a problem in general with validating uniquess in your application logic. Program it in your database to be certain!

class List < ActiveRecord::Base
  has_many :subscribers, :through => :list_subscribers
  has_many :list_subscribers
end

class Subscriber < ActiveRecord::Base has_many :lists has_many :lists, :through => :list_subscribers end

class ListSubscriber< ActiveRecord::Base belongs_to :list belongs_to :subscriber

validates_uniqueness_of :subscriber_id, :scope => :list_id end


Tagged with..
#has_many #models #relationships #through #validates_uniqueness_of #Ruby on Rails

Just finishing up brewing up some fresh ground comments...