Forum: Ruby on Rails validates_uniqueness_of two fields

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.
85e05b7260af81cbacfea7e7ed4f40fb?d=identicon&s=25 Guest (Guest)
on 2006-06-06 18:13
Is it possible to do a validates_uniqueness_of :name, :zip but only in
that context? I'd like to be able to have that zip used again, but not
in conjunction with a name already with that zip.

Does that make sense?
D8f6a28fbdee08dec747b55d16ffc7eb?d=identicon&s=25 Eric Nielsen (nielsene)
on 2006-06-06 20:39
Guest wrote:
> Is it possible to do a validates_uniqueness_of :name, :zip but only in
> that context? I'd like to be able to have that zip used again, but not
> in conjunction with a name already with that zip.
>
> Does that make sense?

I'm new to Rails, but I think validate_uniqueness_of :name, :scope=>:zip
should do what you want.
85e05b7260af81cbacfea7e7ed4f40fb?d=identicon&s=25 Guest (Guest)
on 2006-06-06 22:12
Perfect. Thanks yo.


Eric Nielsen wrote:
> Guest wrote:
>> Is it possible to do a validates_uniqueness_of :name, :zip but only in
>> that context? I'd like to be able to have that zip used again, but not
>> in conjunction with a name already with that zip.
>>
>> Does that make sense?
>
> I'm new to Rails, but I think validate_uniqueness_of :name, :scope=>:zip
> should do what you want.
58c6efb8466b9f85155fe6aa9fc37fce?d=identicon&s=25 Chris T (Guest)
on 2006-06-07 14:05
(Received via mailing list)
Guest wrote:
> Is it possible to do a validates_uniqueness_of :name, :zip but only in
> that context? I'd like to be able to have that zip used again, but not
> in conjunction with a name already with that zip.
>
> Does that make sense?
>
>
>
Do you mean that the zip needs to be unique for each name (i.e. each
name can have only one of each zip), or the other way round. If it's the
former, try:
validates_uniqueness of :zip, :scope => :name
This topic is locked and can not be replied to.