Forum: Ruby on Rails validate_uniqueness_of {combination of 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.
2b891e820c238ded365d035771603f21?d=identicon&s=25 Bill Walton (Guest)
on 2006-03-26 19:23
(Received via mailing list)
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
2b891e820c238ded365d035771603f21?d=identicon&s=25 Bill Walton (Guest)
on 2006-03-26 21:02
(Received via mailing list)
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 Walton
  To: rails@lists.rubyonrails.org
  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
  Rails@lists.rubyonrails.org
  http://lists.rubyonrails.org/mailman/listinfo/rails
2b891e820c238ded365d035771603f21?d=identicon&s=25 Bill Walton (Guest)
on 2006-03-26 21:36
(Received via mailing list)
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 Walton
  To: rails@lists.rubyonrails.org
  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 Walton
    To: rails@lists.rubyonrails.org
    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
    Rails@lists.rubyonrails.org
    http://lists.rubyonrails.org/mailman/listinfo/rails



------------------------------------------------------------------------------


  _______________________________________________
  Rails mailing list
  Rails@lists.rubyonrails.org
  http://lists.rubyonrails.org/mailman/listinfo/rails
2b891e820c238ded365d035771603f21?d=identicon&s=25 Bill Walton (Guest)
on 2006-03-26 22:45
(Received via mailing list)
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 Walton
  To: rails@lists.rubyonrails.org
  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 Walton
    To: rails@lists.rubyonrails.org
    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 Walton
      To: rails@lists.rubyonrails.org
      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
      Rails@lists.rubyonrails.org
      http://lists.rubyonrails.org/mailman/listinfo/rails



----------------------------------------------------------------------------


    _______________________________________________
    Rails mailing list
    Rails@lists.rubyonrails.org
    http://lists.rubyonrails.org/mailman/listinfo/rails



------------------------------------------------------------------------------


  _______________________________________________
  Rails mailing list
  Rails@lists.rubyonrails.org
  http://lists.rubyonrails.org/mailman/listinfo/rails
0727907ae68db2e8ebc1ea1b01f00d69?d=identicon&s=25 Dan Webb (Guest)
on 2006-03-27 00:44
(Received via mailing list)
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 Walton <bill.walton@charter.net> 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
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>
>
>


--
Dan Webb
http://www.danwebb.net
0727907ae68db2e8ebc1ea1b01f00d69?d=identicon&s=25 Dan Webb (Guest)
on 2006-03-27 00:47
(Received via mailing list)
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 Webb <dan@danwebb.net> 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 Webb
> http://www.danwebb.net
>


--
Dan Webb
http://www.danwebb.net
2b891e820c238ded365d035771603f21?d=identicon&s=25 Bill Walton (Guest)
on 2006-03-27 16:18
(Received via mailing list)
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 Webb" <dan@danwebb.net>
To: <rails@lists.rubyonrails.org>
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 Webb <dan@danwebb.net> wrote:
> validates_uniqueness_of :full_name, :scope => "group_id"
>
> That should do it.
>
> Cheers,
>
> Dan
>
> On 3/26/06, Bill Walton <bill.walton@charter.net> wrote:
> >
> > Oops.  Spoke too soon.  While it doesn't throw a syntax error. only the
last
> >
> > ROCKS!
> >
> > ----- Original Message -----
> > From: Bill Walton
> > To: rails@lists.rubyonrails.org
> > 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 Walton
> > To: rails@lists.rubyonrails.org
> > 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 Webb
> http://www.danwebb.net
>


--
Dan Webb
http://www.danwebb.net
_______________________________________________
Rails mailing list
Rails@lists.rubyonrails.org
http://lists.rubyonrails.org/mailman/listinfo/rails
0727907ae68db2e8ebc1ea1b01f00d69?d=identicon&s=25 Dan Webb (Guest)
on 2006-03-27 16:21
(Received via mailing list)
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 Walton <bill.walton@charter.net> 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
> > >
> > > Rails@lists.rubyonrails.org
> > > Rails@lists.rubyonrails.org
> > > Rails@lists.rubyonrails.org
> > >
> http://www.danwebb.net
> _______________________________________________
> Rails mailing list
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
> _______________________________________________
> Rails mailing list
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>


--
Dan Webb
http://www.danwebb.net
59ea1b450935b9d70abfec4186b7a4d5?d=identicon&s=25 Jeff Coleman (progressions)
on 2006-03-28 15:02
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
59ea1b450935b9d70abfec4186b7a4d5?d=identicon&s=25 Jeff Coleman (progressions)
on 2006-03-28 15:04
Jeff Coleman 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
This topic is locked and can not be replied to.