Skip to content

Ensuring your join model uniqueness

July 22, 2008

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

Advertisements
No comments yet

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: