Forum: Ruby on Rails customize validates_uniqueness_of

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
511591894657d7918ecdcaf13a28f40e?d=identicon&s=25 BB (Guest)
on 2006-04-27 22:54
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.
931d5cc3b6fcb9e740ad2846db11a9ba?d=identicon&s=25 Matt Ramos (Guest)
on 2006-04-27 23:05
(Received via mailing list)
Rather than modifying that validation keyword, why not create your own?

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

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.
511591894657d7918ecdcaf13a28f40e?d=identicon&s=25 BB (Guest)
on 2006-04-27 23:20
Matt Ramos wrote:
> Rather than modifying that validation keyword, why not create your own?
>
> http://wiki.rubyonrails.com/rails/pages/HowToAddCu...
>
> 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.
D4ecf6097193e43bf0bf6ea186fcfbd0?d=identicon&s=25 matthibcn (Guest)
on 2006-04-28 00:09
(Received via mailing list)
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
511591894657d7918ecdcaf13a28f40e?d=identicon&s=25 BB (Guest)
on 2006-04-28 00:30
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.
This topic is locked and can not be replied to.