Customize validates_uniqueness_of


#1

I need to modify the validates_uniqueness_of function so that it will
check that the string is unique but only in its own category.

For example right now I have

validates_uniqueness_of :title

so let’s say I have the :title “Title 1” already in the database with
the :category of “1”

then if i wanted to enter the :title “Title 1” in the database again BUT
with a :category of “2” I need it to go through.

In other words it should only create an error if I try to enter :title
“Title 1” AND :category “1”

Thanks in advance for any suggestions.


#2

Rather than modifying that validation keyword, why not create your own?

http://wiki.rubyonrails.com/rails/pages/HowToAddCustomValidationKeywords

This may help with what you want to do, I’m not sure on what you’d have
to
put in the newly created keyword, but hopefully it’ll get you on the
right
track.


#3

Matt R. wrote:

Rather than modifying that validation keyword, why not create your own?

http://wiki.rubyonrails.com/rails/pages/HowToAddCustomValidationKeywords

This may help with what you want to do, I’m not sure on what you’d have
to
put in the newly created keyword, but hopefully it’ll get you on the
right
track.

Thanks Matt,

I’ll take a look at that. I was just hoping there was a way to modify
the existing validates function.


#4

BB wrote:

then if i wanted to enter the :title “Title 1” in the database again BUT
with a :category of “2” I need it to go through.

In other words it should only create an error if I try to enter :title
“Title 1” AND :category “1”

Thanks in advance for any suggestions.

validates_uniqueness_of :title, :scope => “category”

Cheers

Matthias Oesterle


#5

matthibcn wrote:

BB wrote:

then if i wanted to enter the :title “Title 1” in the database again BUT
with a :category of “2” I need it to go through.

In other words it should only create an error if I try to enter :title
“Title 1” AND :category “1”

Thanks in advance for any suggestions.

validates_uniqueness_of :title, :scope => “category”

Cheers

Matthias Oesterle

Damn that’s cool. Thanks a lot.