Rails Model multiple column uniqueness
Posted
by Jty.tan
on Stack Overflow
See other posts from Stack Overflow
or by Jty.tan
Published on 2010-05-20T08:30:43Z
Indexed on
2010/05/21
2:20 UTC
Read the original article
Hit count: 278
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
?
© Stack Overflow or respective owner