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.
Bill W. (Guest)
on 2006-03-26 21: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
Bill W. (Guest)
on 2006-03-26 23: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 W.
  To: removed_email_address@domain.invalid
  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
  removed_email_address@domain.invalid
  http://lists.rubyonrails.org/mailman/listinfo/rails
Bill W. (Guest)
on 2006-03-26 23: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 W.
  To: removed_email_address@domain.invalid
  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: removed_email_address@domain.invalid
    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
    removed_email_address@domain.invalid
    http://lists.rubyonrails.org/mailman/listinfo/rails



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


  _______________________________________________
  Rails mailing list
  removed_email_address@domain.invalid
  http://lists.rubyonrails.org/mailman/listinfo/rails
Bill W. (Guest)
on 2006-03-27 00: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 W.
  To: removed_email_address@domain.invalid
  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: removed_email_address@domain.invalid
    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: removed_email_address@domain.invalid
      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
      removed_email_address@domain.invalid
      http://lists.rubyonrails.org/mailman/listinfo/rails



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


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



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


  _______________________________________________
  Rails mailing list
  removed_email_address@domain.invalid
  http://lists.rubyonrails.org/mailman/listinfo/rails
Dan W. (Guest)
on 2006-03-27 02: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 W. <removed_email_address@domain.invalid> 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
> removed_email_address@domain.invalid
> http://lists.rubyonrails.org/mailman/listinfo/rails
>
>
>


--
Dan W.
http://www.danwebb.net
Dan W. (Guest)
on 2006-03-27 02: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 W. <removed_email_address@domain.invalid> 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
Bill W. (Guest)
on 2006-03-27 18: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 W." <removed_email_address@domain.invalid>
To: <removed_email_address@domain.invalid>
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. <removed_email_address@domain.invalid> wrote:
> validates_uniqueness_of :full_name, :scope => "group_id"
>
> That should do it.
>
> Cheers,
>
> Dan
>
> On 3/26/06, Bill W. <removed_email_address@domain.invalid> wrote:
> >
> > Oops.  Spoke too soon.  While it doesn't throw a syntax error. only the
last
> >
> > ROCKS!
> >
> > ----- Original Message -----
> > From: Bill W.
> > To: removed_email_address@domain.invalid
> > 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: removed_email_address@domain.invalid
> > 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
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails
Dan W. (Guest)
on 2006-03-27 18: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 W. <removed_email_address@domain.invalid> 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
> > >
> > > removed_email_address@domain.invalid
> > > removed_email_address@domain.invalid
> > > removed_email_address@domain.invalid
> > >
> http://www.danwebb.net
> _______________________________________________
> Rails mailing list
> removed_email_address@domain.invalid
> http://lists.rubyonrails.org/mailman/listinfo/rails
> _______________________________________________
> Rails mailing list
> removed_email_address@domain.invalid
> http://lists.rubyonrails.org/mailman/listinfo/rails
>


--
Dan W.
http://www.danwebb.net
Jeff C. (Guest)
on 2006-03-28 17: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
Jeff C. (Guest)
on 2006-03-28 17:04
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
This topic is locked and can not be replied to.