Ensuring your join model uniqueness

2008 July 22

For example:

id |category_id | inventory_id

1   384            1            #first entry
2   384            2            #this would be ok.
3   384            1            #this would not be ok

To ensure that a category_id doesn’t have any inventory_id duplicate:


class CategoryProduct < ActiveRecord::Base
  belongs_to :category
  belongs_to :inventory

  validates_uniqueness_of :category_id, :scope => :inventory_id
end
No comments yet

Leave a Reply

Note: You can use basic XHTML in your comments. Your email address will never be published.

Subscribe to this comment feed via RSS