Ensuring your join model uniqueness

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:

[sourcecode language="ruby"]

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

  validates_uniqueness_of :category_id, :scope => :inventory_id
end

[/sourcecode]

Tags: , , ,

No comments yet.

Leave a Reply