Validate_uniqueness_of {combination of fields}?

I’ve looked at the validates documentation and it appears that
validates_uniqueness_of works on only one field at a time. So I can,
for example, validate that “sam” is unique to the record set, and that
“smith” is unique to the record set.

My problem is that each record includes two fields, the combination of
which must be unique to the record set, but each of which may not be
unique. So “sam smith”, “mary smith” and “sam jones” (stored as fname,
lname) all need to pass the validation.

Is there an easy way to validate_uniqueness_of a combination of fields
within a record?

Thanks in advance for any help.

Best regards,
Bill

I’ve realized in rereading my post below that I could have stated the
problem I’m having more clearly. The system is multi-user, so the
record set within which I’m trying to validate_uniqueness_of is
constrained by a third field (e.g., ‘group_id’). So I’m already using
the “scope” option on validates_uniqueness_of. I think what I’m looking
for is nested scoping. Something like:

validates_uniqueness_of :fname, :scope => “lname”, :scope => “group_id”

Is there an easy (i.e., Rails) way to accomplish this?

Thanks,
Bill

----- Original Message -----
From: Bill W.
To: [email protected]
Sent: 2006-03-26 11:08 AM
Subject: [Rails] validate_uniqueness_of {combination of fields} ???

I’ve looked at the validates documentation and it appears that
validates_uniqueness_of works on only one field at a time. So I can,
for example, validate that “sam” is unique to the record set, and that
“smith” is unique to the record set.

My problem is that each record includes two fields, the combination
of which must be unique to the record set, but each of which may not be
unique. So “sam smith”, “mary smith” and “sam jones” (stored as fname,
lname) all need to pass the validation.

Is there an easy way to validate_uniqueness_of a combination of fields
within a record?

Thanks in advance for any help.

Best regards,
Bill



Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails

I’ll be damned. The nested scoping works just the way I wrote it below.
I’m posting this just so another newbie might be spared the angst. RoR
ROCKS!
----- Original Message -----
From: Bill W.
To: [email protected]
Sent: 2006-03-26 12:47 PM
Subject: Re: [Rails] validate_uniqueness_of {combination of fields}
???

I’ve realized in rereading my post below that I could have stated the
problem I’m having more clearly. The system is multi-user, so the
record set within which I’m trying to validate_uniqueness_of is
constrained by a third field (e.g., ‘group_id’). So I’m already using
the “scope” option on validates_uniqueness_of. I think what I’m looking
for is nested scoping. Something like:

validates_uniqueness_of :fname, :scope => “lname”, :scope =>
“group_id”

Is there an easy (i.e., Rails) way to accomplish this?

Thanks,
Bill

----- Original Message -----
From: Bill W.
To: [email protected]
Sent: 2006-03-26 11:08 AM
Subject: [Rails] validate_uniqueness_of {combination of fields} ???


I've looked at the validates documentation and it appears that 

validates_uniqueness_of works on only one field at a time. So I can,
for example, validate that “sam” is unique to the record set, and that
“smith” is unique to the record set.

My problem is that each record includes two fields, the 

combination of which must be unique to the record set, but each of
which may not be unique. So “sam smith”, “mary smith” and “sam jones”
(stored as fname, lname) all need to pass the validation.

Is there an easy way to validate_uniqueness_of a combination of 

fields within a record?

Thanks in advance for any help.

Best regards,
Bill

_______________________________________________
Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails

Hi Bill,

I think you can achieve what you are after by defining a method like so:

def fullname
[fname, lname]. join ’ ’
end

then validate that:

validates_uniqueness_of :full_name, :scope => “group_id”

That should do it.

Cheers,

Dan

On 3/26/06, Bill W. [email protected] wrote:

Bill
I’ll be damned. The nested scoping works just the way I wrote it below.
I’ve realized in rereading my post below that I could have stated the
Thanks,


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails


Dan W.
http://www.danwebb.net

Oops. Spoke too soon. While it doesn’t throw a syntax error. only the
last “:scope => x” option is acted upon. So the question remains…

Is there an easy way to accomplish nested scoping?

Thanks much for any advice.

Best regards,
Bill
----- Original Message -----
From: Bill W.
To: [email protected]
Sent: 2006-03-26 1:21 PM
Subject: Re: [Rails] validate_uniqueness_of {combination of fields}
???

I’ll be damned. The nested scoping works just the way I wrote it
below. I’m posting this just so another newbie might be spared the
angst. RoR ROCKS!
----- Original Message -----
From: Bill W.
To: [email protected]
Sent: 2006-03-26 12:47 PM
Subject: Re: [Rails] validate_uniqueness_of {combination of fields}
???

I've realized in rereading my post below that I could have stated 

the problem I’m having more clearly. The system is multi-user, so the
record set within which I’m trying to validate_uniqueness_of is
constrained by a third field (e.g., ‘group_id’). So I’m already using
the “scope” option on validates_uniqueness_of. I think what I’m looking
for is nested scoping. Something like:

validates_uniqueness_of :fname, :scope => "lname", :scope => 

“group_id”

Is there an easy (i.e., Rails) way to accomplish this?

Thanks,
Bill


  ----- Original Message -----
  From: Bill W.
  To: [email protected]
  Sent: 2006-03-26 11:08 AM
  Subject: [Rails] validate_uniqueness_of {combination of fields} 

???

  I've looked at the validates documentation and it appears that 

validates_uniqueness_of works on only one field at a time. So I can,
for example, validate that “sam” is unique to the record set, and that
“smith” is unique to the record set.

  My problem is that each record includes two fields, the 

combination of which must be unique to the record set, but each of
which may not be unique. So “sam smith”, “mary smith” and “sam jones”
(stored as fname, lname) all need to pass the validation.

  Is there an easy way to validate_uniqueness_of a combination of 

fields within a record?

  Thanks in advance for any help.

  Best regards,
  Bill

  _______________________________________________
  Rails mailing list
  [email protected]
  http://lists.rubyonrails.org/mailman/listinfo/rails

_______________________________________________
Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails

oops, theres typos in that code. I should go to bed. Here it is again:

def full_name
[fname, lname].join ’ ’
end

validates_uniqueness_of :full_name, :scope => “group_id”

On 3/26/06, Dan W. [email protected] wrote:

validates_uniqueness_of :full_name, :scope => “group_id”

“:scope => x” option is acted upon. So the question remains…
----- Original Message -----

third field (e.g., ‘group_id’). So I’m already using the “scope” option on

validates_uniqueness_of works on only one field at a time. So I can, for
within a record?

http://lists.rubyonrails.org/mailman/listinfo/rails


Dan W.
http://www.danwebb.net


Dan W.
http://www.danwebb.net

Hi Dan,

Thanks much for the assist, but it doesn’t work. That is, the
validate_uniqueness_of fails when AR can’t find a column named
“full_name”.
If I’ve missed some nuance of your recommendation (wouldn’t surprise me
to
get a ‘hey newbie, of course you have to do X in addition to what I sent
you’), please set me straight. At this point, I guess I’m stuck with
having
to go the ugly route.

Thanks much for your response, though.

Best regards,
Bill

----- Original Message -----
From: “Dan W.” [email protected]
To: [email protected]
Sent: 2006-03-26 4:46 PM
Subject: Re: [Rails] validate_uniqueness_of {combination of fields} ???

oops, theres typos in that code. I should go to bed. Here it is again:

def full_name
[fname, lname].join ’ ’
end

validates_uniqueness_of :full_name, :scope => “group_id”

On 3/26/06, Dan W. [email protected] wrote:

validates_uniqueness_of :full_name, :scope => “group_id”

That should do it.

Cheers,

Dan

On 3/26/06, Bill W. [email protected] wrote:

Oops. Spoke too soon. While it doesn’t throw a syntax error. only the
last

ROCKS!

----- Original Message -----
From: Bill W.
To: [email protected]
Sent: 2006-03-26 12:47 PM
Subject: Re: [Rails] validate_uniqueness_of {combination of fields} ???

I’ve realized in rereading my post below that I could have stated the
problem I’m having more clearly. The system is multi-user, so the
record
set within which I’m trying to validate_uniqueness_of is constrained by
a
third field (e.g., ‘group_id’). So I’m already using the “scope” option
on
validates_uniqueness_of. I think what I’m looking for is nested
scoping.

----- Original Message -----
From: Bill W.
To: [email protected]
Sent: 2006-03-26 11:08 AM
Subject: [Rails] validate_uniqueness_of {combination of fields} ???

I’ve looked at the validates documentation and it appears that
validates_uniqueness_of works on only one field at a time. So I can,
for
example, validate that “sam” is unique to the record set, and that
“smith”
is unique to the record set.

My problem is that each record includes two fields, the combination of
which must be unique to the record set, but each of which may not be
unique.
So “sam smith”, “mary smith” and “sam jones” (stored as fname, lname)
all


Dan W.
http://www.danwebb.net


Dan W.
http://www.danwebb.net


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails

Looks like Rails 1.1 has the answer. From
http://scottraymond.net/articles/2006/02/28/rails-1.1:

“Allow validate_uniqueness_of to be scoped by multiple columns. See
http://dev.rubyonrails.org/ticket/1559

Jeff

Jeff C.man should have written:

Looks like Rails 1.1 has the answer. From
http://scottraymond.net/articles/2006/02/28/rails-1.1 :

"Allow validate_uniqueness_of to be scoped by multiple columns. See
http://dev.rubyonrails.org/ticket/1559 "

Jeff

Sorry, yes, that isn’t going to work is it. I’ll think on it more.
Shouldn’t try to answer questions at 3 in the morning again.

On 3/27/06, Bill W. [email protected] wrote:

Hi Bill,

“:scope => x” option is acted upon. So the question remains…
----- Original Message -----

set within which I’m trying to validate_uniqueness_of is constrained by

which must be unique to the record set, but each of which may not be

[email protected]
[email protected]
[email protected]

http://www.danwebb.net


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails


Dan W.
http://www.danwebb.net