Validates_uniqueness_of multiple field combo?


#1

Hello,

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?


#2

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
would
put the validation. Perhaps in the User model you could put the
validation
above.