Forum: Ruby on Rails Validates_uniqueness_of multiple field combo?

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Bonicon (Guest)
on 2006-05-10 06:37

I am new to Ruby on rails and this is therefore probably trivial. Lets
say I have 3 tables - users[id,name, age, ...],
interests[id,name,description, ...], and finally a table
user_interests[id,user_id,interest_id] to link a user to a set of
interests. Now there is no problem to put validates_uniqueness_of :name
in the user-model to ensure that I only have one Lenny, but how do I
validate the uniqueness of Lenny's interests in the user_interest-table?
Ie, is there a method to validate that an entry into a table is unique
in terms of several fields? Lenny -fishing, Lenny - programming OK,
whereas Lenny - fishing, Lenny - fishing would be caught?
Daniel -. (Guest)
on 2006-05-10 07:32
(Received via mailing list)
validates_uniqueness_of has a :scope option that works like
validates_uniqueness_of :interest, :scope => :user

but I'm not sure how this works with HABTM relationships or where you
put the validation.  Perhaps in the User model you could put the
This topic is locked and can not be replied to.