views:

26

answers:

1

I am making a Viewer model with

belongs_to :users
belongs_to :orders

that joins the models Users and Orders with a :has_many :through => :viewers.

And the Viewer model has the attributes of user_id and order_id.

How would I set it up so that new viewers are only accepted if both user_id and order_id are unique in the same row? I remember in MySQL being able to do so with a flag (although I can't for the life of me remember what it was), but I'm not sure how to do it with Rails.

Can I do something like (for Viewer.rb) validates_uniqueness_of :user_id, :scope => :order_id?

A: 

Oh. I think the way to do so is as follows:

in the Viewer model migration file (ie: I should have done this earlier)

def self.up
  #create_table code
  end

  add_index :viewers, [:user_id, :order_id], :unique => true
end
Jty.tan