Rails Model multiple column uniqueness
- by Jty.tan
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?